The CSS rgb() function mixes red, green, and blue light to make a color. Think of it as three small color controls: raise or lower each control, and the resulting color changes. Each component can be written as a decimal value from 0 to 255, giving more than 16 million possible colors. section 17.3
Write rgb() with three values, in this order:
- Red first: 0 means no red; 255 means full red. section 17.3
- Green second: 0 means no green; 255 means full green. section 17.3
- Blue third: 0 means no blue; 255 means full blue. section 17.3
Make heading text purple
Use rgb(128, 0, 128) as a color property value. The equal red and blue values produce purple, while green is set to zero. h1 { color: rgb(128, 0, 128); } This rule selects every h1 element and gives its text a purple color. section 17.3 section 17.4
Percentages are another way to supply the three components. Here, 100% has the same full intensity as 255, and 0% has the same intensity as 0. Purple can therefore be written as rgb(50%, 0, 50%). Common combinations include red as rgb(100%, 0, 0), yellow as rgb(100%, 100%, 0), black as rgb(0, 0, 0), and white as rgb(100%, 100%, 100%). section 17.4 section 17.5
h1 { #1
color: rgb(128, 0, 128); #2
}
Inspect the short CSS rule: the selector identifies the headings, and rgb() supplies the color value. section 17.4
Use rgb() wherever a CSS color value is needed, such as the color property for text or background-color for an element’s background. If choosing numbers feels less visual than choosing paint, a color chooser can display a selected color as decimal rgb(), percentage rgb(), HSL, hexadecimal, or a keyword when available. A color-scheme calculator can also accept rgb() as an input and display related colors in several notations. section 17.29 section 17.14 section 17.16
rgb() controls solid color only. For partly transparent text, use rgba(), which adds an alpha channel from 0.0 (fully transparent) to 1.0 (fully opaque). Transparency can add visual interest, but text is usually kept solid for readability. section 17.9


