rgb() - CSS: Cascading Style Sheets | MDN (2024)

The rgb() functional notation expresses a color in the sRGB color space according to its red, green, and blue components. An optional alpha component represents the color's transparency.

Try it

Syntax

css

/* Absolute values */rgb(255 255 255)rgb(255 255 255 / 50%)/* Relative values */rgb(from green r g b / 0.5)rgb(from #0000FF calc(r + 40) calc(g + 40) b)rgb(from hwb(120deg 10% 20%) r g calc(b + 200))

The rgba() function can also be used to express sRGB colors. This is an alias for rgb() that accepts the same parameters.

Note: rgb()/rgba() can also be written in a legacy form in which all values are separated with commas, for example rgb(255, 0, 0). Mixing number and percent value types is not valid in the comma-separated legacy syntax (i.e. the R, G, and B values must be either all numbers or all percentages), and the none value is also not permitted.

Values

Below are descriptions of the allowed values for both absolute and relative colors.

Absolute value syntax

rgb(R G B[ / A])

The parameters are as follows:

R, G, B

Each value can be represented as a <number> between 0 and 255, a <percentage> between 0% and 100%, or the keyword none (equivalent to 0% in this case). These values represent the red, green, and blue channels, respectively.

A Optional

An <alpha-value> representing the alpha channel value of the color, where the number 0 corresponds to 0% (fully transparent) and 1 corresponds to 100% (fully opaque). Additionally, the keyword none can be used to explicitly specify no alpha channel. If the A channel value is not explicitly specified, it defaults to 100%. If included, the value is preceded by a slash (/).

Note: See Missing color components for more information on the effect of none.

Relative value syntax

rgb(from <color> R G B[ / A])

The parameters are as follows:

from <color>

The keyword from is always included when defining a relative color, followed by a <color> value representing the origin color: This is the original color that the relative color is based on. The origin color can be any valid <color> syntax, including another relative color.

R, G, B

Each value can be represented as a <number> between 0 and 255, a <percentage> between 0% and 100%, or the keyword none (equivalent to 0% in this case). These values represent the red, green, and blue channel values of the output color, respectively.

A Optional

An <alpha-value> representing the alpha channel value of the output color, where the number 0 corresponds to 0% (fully transparent) and 1 corresponds to 100% (fully opaque). Additionally, the keyword none can be used to explicitly specify no alpha channel. If the A channel value is not explicitly specified, it defaults to the alpha channel value of the origin color. If included, the value is preceded by a slash (/).

Note: The rgba() alias can also be used to output relative colors, and to specify origin colors. When using rgba() to output a relative color, you must use the comma-less modern syntax.

Note: To fully enable the representation of the full spectrum of visible colors, the output of relative rgb() color functions is serialized to color(srgb). That means that querying the output color value via the HTMLElement.style property or the CSSStyleDeclaration.getPropertyValue() method returns the output color as a color(srgb ...) value.

Defining relative color output channel components

When using relative color syntax inside an rgb() function, the browser converts the origin color into an equivalent RGB color (if it is not already specified as such). The color is defined as three distinct color channel values — r (red), g (green), and b (blue) — plus an alpha channel value (alpha). These channel values are made available inside the function to be used when defining the output color channel values:

  • The r, g and b values are each resolved to <number>s between 0 and 255, inclusive.
  • The alpha channel is resolved to a <number> between 0 and 1, inclusive.

When defining a relative color, the different channels of the output color can be expressed in several different ways. Below, we'll study some examples to illustrate these.

In the first two examples below, we are using relative color syntax. However, the first one outputs the same color as the origin color and the second one outputs a color not based on the origin color at all. They don't really create relative colors! You'd be unlikely to ever use these in a real codebase, and would probably just use an absolute color value instead. We included these examples as a starting point for learning about relative rgb() syntax.

Let's start with an origin color of hsl(0 100% 50%) (equivalent to rgb(255 0 0)). The following function outputs the same color as the origin color — it uses the origin color's r, g, and b channel values (255, 0, and 0) as the output channel values:

css

rgb(from hsl(0 100% 50%) r g b)

This function's output color is the sRGB color() equivalent of rgb(255 0 0): color(srgb 1 0 0).

The next function uses absolute values for the output color's channel values, outputting a completely different color not based on the origin color:

css

rgb(from hsl(0 100% 50%) 132 132 224)

In the above case, the output color is the sRGB color() equivalent of rgb(132 132 224): color(srgb 0.517647 0.517647 0.878431).

The following function creates a relative color based on the origin color:

css

rgb(from hsl(0 100% 50%) r 80 80)

This example:

  • Converts the origin color (hsl(0 100% 50%)) into an rgb() equivalent (rgb(255 0 0)).
  • Sets the R channel value for the output color to the origin color rgb() equivalent's R channel value — 255.
  • Sets the output color's G and B channel values to new values not based on the origin color: 80 and 80, respectively.

The final output color is the equivalent of rgb(255 80 80) in the sRGB color space — color(srgb 1 0.313726 0.313726).

Note: As mentioned above, if the output color is using a different color model to the origin color, the origin color is converted to the same model or space as the output color in the background so that it can be represented in a way that is compatible (i.e. using the same channels).

In the examples we've seen so far in this section, the alpha channels have not been explicitly specified for either the origin or output colors. When the output color alpha channel is not specified, it defaults to the same value as the origin color alpha channel. When the origin color alpha channel is not specified (and it is not a relative color), it defaults to 1. Therefore, the origin and output alpha channel values are 1 for the above examples.

Let's look at some examples that specify origin and output alpha channel values. The first one specifies the output alpha channel value as being the same as the origin alpha channel value, whereas the second one specifies a different output alpha channel value, unrelated to the origin alpha channel value.

css

rgb(from hsl(0 100% 50% / 0.8) r g b / alpha)/* Computed output color: color(srgb 1 0 0 / 0.8) */rgb(from hsl(0 100% 50% / 0.8) r g b / 0.5)/* Computed output color: color(srgb 1 0 0 / 0.5) */

In the following example, the hsl() origin color is again converted into an rgb() representation — rgb(255 0 0). calc() calculations are applied to the R, G, B, and A values. After calculating, the R, G, B and A values are 127.5, 25, 175, and0.9 respectively. The final output color is the equivalent of rgb(127.5 25 175 / 0.9) in the sRGB color space: color(srgb 0.5 0.0980392 0.686275 / 0.9).

css

rgb(from hsl(0 100% 50%) calc(r/2) calc(g + 25) calc(b + 175) / calc(alpha - 0.1))

Note: Because the origin color channel values are resolved to <number> values, you have to add numbers to them when using them in calculations, even in cases where a channel would normally accept <percentage>, <angle>, or other value types. Adding a <percentage> to a <number>, for example, doesn't work.

Formal syntax

<rgb()> = 
<legacy-rgb-syntax> |
<modern-rgb-syntax>

<legacy-rgb-syntax> =
rgb( <percentage>#{3} , <alpha-value>? ) |
rgb( <number>#{3} , <alpha-value>? )

<modern-rgb-syntax> =
rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? )

<alpha-value> =
<number> |
<percentage>

Examples

Basic syntax

In this example, we have three <div> elements with different background colors displayed over a striped background.

HTML

html

<div> <div id="integer"></div> <div id="percent"></div> <div id="alpha"></div></div>

CSS

The background colors are set using the rgb() color function. The three colors are the same. The third is semi-transparent, so we included a repeating-linear-gradient() on the <body> to better demonstrate the transparency of alpha channels.

div { display: flex; gap: 20px; height: 50px; flex: 1;}body { height: calc(100vh - 20px);}

css

body { background: repeating-linear-gradient(-45deg, #eee 0 2px, #fff 2px 6px); padding: 10px;}#integer { background-color: rgb(189 85 218);}#percent { background-color: rgb(74% 33% 85%);}#alpha { background-color: rgb(189 85 218 / 0.25);}

Result

Using relative colors with rgb()

This example styles three <div> elements with different background colors. The left-hand one is given the unmodified --base-color, while the middle and right ones are given variants of that --base-color that successively remove more from the red channel and add more to the blue channel.

These variants are defined using relative colors — the --base-color custom property is passed into an rgb() function, and the output color has its red and blue channels modified to achieve the desired effect via calc() functions, while the green channel is left unchanged.

<div id="container"> <div class="item" id="one"></div> <div class="item" id="two"></div> <div class="item" id="three"></div></div>

CSS

#container { display: flex; width: 100vw; height: 100vh; box-sizing: border-box;}.item { flex: 1; margin: 20px;}

css

:root { --base-color: orange; /* equal to rgb(255 165 0) */}#one { background-color: var(--base-color);}#two { background-color: rgb(from var(--base-color) calc(r - 76.5) g calc(b + 76.5)); /* 76.5 is 30% of 255 */}#three { background-color: rgb(from var(--base-color) calc(r - 153) g calc(b + 153)); /* 153 is 60% of 255 */}/* Use @supports to add in support for old syntax that requires r g b values to be specified as percentages (with units) in calculations. This is required for Safari 16.4+. */@supports (color: rgb(from red r g calc(b + 30%))) { #two { background-color: rgb(from var(--base-color) calc(r - 30%) g calc(b + 30%)); } #three { background-color: rgb(from var(--base-color) calc(r - 60%) g calc(b + 60%)); }}

Result

Legacy syntax: comma-separated values

For legacy reasons, the rgb() function accepts a form in which all values are separated using commas.

HTML

html

<div class="space-separated"></div><div class="comma-separated"></div>

CSS

css

div { width: 100px; height: 50px; margin: 1rem;}div.space-separated { background-color: rgb(255 0 0 / 50%);}div.comma-separated { background-color: rgb(255, 0, 0, 0.5);}

Result

rgba() syntax

The rgba() syntax is an alias for rgb().

HTML

html

<div class="rgb"></div><div class="rgba"></div>

CSS

css

div { width: 100px; height: 50px; margin: 1rem;}div.rgb { background-color: rgb(255 0 0 / 50%);}div.rgba { background-color: rgba(255 0 0 / 0.5);}

Result

Specifications

Specification
CSS Color Module Level 5
# relative-RGB
CSS Color Module Level 4
# rgb-functions

Browser compatibility

BCD tables only load in the browser

See also

  • The <color> data type for a list of all color notations
  • sRGB color picker and conversion tool
  • Using relative colors
  • CSS colors module
rgb() - CSS: Cascading Style Sheets | MDN (2024)

FAQs

How to give RGB values in CSS? ›

CSS rgb() Function

An RGB color value is specified with: rgb(red, green, blue). Each parameter defines the intensity of that color and can be an integer between 0 and 255 or a percentage value (from 0% to 100%).

What is RGB 255 255 255? ›

255, 255, 255 is white.

Does RGB work in CSS? ›

The background colors are set using the rgb() color function. The three colors are the same. The third is semi-transparent, so we included a repeating-linear-gradient() on the <body> to better demonstrate the transparency of alpha channels.

How to make text RGB in CSS? ›

CSS RGB Colors

The RGB colors are specified with a comma-separated list of three numeric values either ranging from 0 to 255 or percentage values ranging from 0% to 100%. For example, the rgb(255, 0, 0) is equivalent to rgb(100%, 0, 0) . Note: The combination of rgb values can generate millions of different colors.

How to use RGB in HTML? ›

RGB Color Values

For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255), and the other two (green and blue) are set to 0. Another example, rgb(0, 255, 0) is displayed as green, because green is set to its highest value (255), and the other two (red and blue) are set to 0.

How do you list RGB values? ›

So, a full RGB color number in hexadecimal notation consists of six digits. The first two digits in the RGB color number represent the amount of red, the second two digits the amount of green, and the final two digits the amount of blue.

How do cascading style sheets CSS allow RGB values to be entered? ›

Cascading Style Sheets, known as CSS, allow colors to be specified using RGB values in hexadecimal notation. This means that colors are coded as #, with representing the amount of red, for green, and for blue. These hexadecimal values range from 00 to FF.

What is the alternative to RGB in CSS? ›

Hexadecimal Color Values

Probably the most common (yet least intuitive) way to specify colors in CSS is to use their hexadecimal (or hex) values. Hex values are actually just a different way to represent RGB values. Instead of using three numbers between 0 and 255 , you use six hexadecimal numbers.

How does RGB work? ›

Every pixel in a display is composed of red, green and blue (RGB) subpixels that light up at different intensities to create different colors. This color space is a color representation method used in electronic displays, like televisions, computer monitors, digital cameras and various types of lighting.

What is the difference between RGB and rgba? ›

RGB is a 3-channel format containing data for Red, Green, and Blue. RGBA is a 4-channel format containing data for Red, Green, Blue, and Alpha. There is no use to the Alpha channel other than making the color transparent/opaque (or partially transparent; translucent).

How to convert hex color to RGB CSS? ›

Converting from hexadecimal values to rgb() values is simple. For every six-character hexadecimal color, simply take the first number of every pair (there are three pairs), multiply it by 16, then add the second number of the pair.

How to add a picture in CSS? ›

To add images to a page, we use the <img> inline element. The <img> element is a self-containing, or empty, element, which means that it doesn't wrap any other content and it exists as a single tag. For the <img> element to work, a src attribute and value must be included to specify the source of the image.

How to put color code in CSS? ›

Simply add the appropriate CSS selector and define the color property with the value you want. For example, say you want to change the color of all paragraphs on your site to navy. Then you'd add p {color: #000080; } to the head section of your HTML file.

How to get CSS color value? ›

The value in the CSS color property can be expressed as a hexadecimal value, rgb value, or as a named color. Color values can be expressed in hexadecimal values such as #FFFFFF, #000000, and #FF0000. Color values can be expressed using rgb such as rgb(255,255,255), rgb(0,0,0), and rgb(255,0,0).

How do I get RGB values from my screen? ›

Click the 'print screen' button on your keyboard to take a snapshot of your screen. Paste the image into MS Paint. 2. Click on the color selector icon (the eyedropper), and then click on the color of in- terest to select it, then click on 'edit color'.

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 6019

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.