rgb()
Writes a colour as a mix of red, green, and blue light, with optional transparency. It also covers hex notation.
Example
#.btn {
background: rgb(124 58 237);
box-shadow: 0 8px 20px rgb(124 58 237 / 0.35);
}
.btn.ghost {
background: rgb(124 58 237 / 0.15);
box-shadow: none;
color: rgb(196 181 253);
}<div style="display: flex; flex-wrap: wrap; gap: 12px; padding: 4px 0 16px;">
<button class="btn" type="button" style="padding: 10px 18px; border: 0; border-radius: 10px; color: #fff; font: 600 15px system-ui, sans-serif;">Start the lesson</button>
<button class="btn ghost" type="button" style="padding: 10px 18px; border: 0; border-radius: 10px; font: 600 15px system-ui, sans-serif;">Later</button>
</div>The shadow and the second buttonβs background are the same purple as the first buttonβs background, just with an alpha channel. You donβt need to pick separate colours.
Definition and usage
#A screen builds every colour from three lights: red, green, and blue. The rgb() function gives the intensity of each on a scale from 0 to 255 or from 0% to 100%: rgb(0 0 0) is black, rgb(255 255 255) is white, and rgb(124 58 237) is purple. The larger the numbers, the lighter the colour, because the lights add up.
You add transparency, the alpha channel, after a slash: rgb(124 58 237 / 0.5) or rgb(124 58 237 / 50%). The older comma syntax, rgb(124, 58, 237), and the rgba() function still work and mean the same. Modern rgb() accepts alpha without an extra letter in the name.
The hexadecimal notation #7c3aed is the same three components in base 16: two digits from 00 to ff for red, green, and blue. The shorthand #fff expands to #ffffff, and a fourth pair of digits is alpha: #7c3aed80 means about 50% opacity. Hex and rgb() describe exactly the same colours, so the choice is a matter of convenience.
The drawback of RGB is that it is hard to think about colours in it: you canβt immediately see how to lighten a shade or make a paler version of it. For building palettes hsl() and oklch() are more convenient, and for mixing colours color-mix().
Syntax
#rgb(<red> <green> <blue> [/ <alpha>]?)
#rrggbb #rrggbbaa #rgb #rgbaSyntax variants
#purple, full opacity
Three numbers from 0 to 255: a fair amount of red, little green, and the most blue.the same purple, half opacity
Alpha after a slash. The checkerboard shows through the colour, while the text stays fully visible.orange in percentages
Components in percentages instead of numbers. 100% is 255 and 60% is 153, giving the colour 255, 153, 0.emerald in hex
Hexadecimal notation:10of red,b9of green, and81of blue, which isrgb(16 185 129).shorthand for #ff4433
Three digits are a shorthand in which each digit is doubled:#f43means#ff4433.hex with alpha
The fourth pair of digits is alpha:80in hexadecimal is 128 out of 255, which is about 50% opacity.older syntax
The former notation with commas and the letter βaβ in the name. It still works, but in new codergb()with a slash is enough.
Syntax variants in practice
#rgb(124 58 237)
.demo { background: rgb(124 58 237); }rgb(124 58 237 / 50%)
.demo { background: rgb(124 58 237 / 50%); }rgb(100% 60% 0%)
.demo { background: rgb(100% 60% 0%); }#10b981
10 of red, b9 of green, and 81 of blue, which is rgb(16 185 129)..demo { background: #10b981; }#f43
#f43 means #ff4433..demo { background: #f43; }#0ea5e980
80 in hexadecimal is 128 out of 255, which is about 50% opacity..demo { background: #0ea5e980; }rgba(14, 165, 233, 0.6)
rgb() with a slash is enough..demo { background: rgba(14, 165, 233, 0.6); }More examples
#:root {
--brand-rgb: 167 139 250;
}
.chip {
border: 1px solid rgb(var(--brand-rgb));
background: rgb(var(--brand-rgb) / 0.15);
color: rgb(var(--brand-rgb));
}<span class="chip" style="display: inline-block; padding: 4px 12px; border-radius: 999px; font-weight: 600;">Module 3: Flexbox</span>The variable holds just the components, without the function name. This way you can add a different transparency in every place.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- When only the background should be transparent, use a colour with alpha instead of
opacity. Theopacityproperty fades the entire element together with its text and children. - You will usually get colours from design tools, for example Figma, in hex. You can paste them without converting, because hex and
rgb()are equivalent. - Donβt mix commas and spaces in a single call. The expression
rgb(124, 58 237)is invalid and the browser will drop it.
Related entries
#- hsl()Writes a colour through its hue on the colour wheel, saturation, and lightness. It makes it easy to lighten, darken, or mute a colour.
- oklch()Writes a colour through lightness, chroma, and hue in a space matched to the eye. Colours of the same lightness look equally light.
- color-mix()Mixes two colours in given proportions and in a chosen colour space. It creates lighter, darker, and semi-transparent variants.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- background-colorSets the background colour of an element.
- opacitySets the transparency of the whole element together with its contents.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.