rgb()

Writes a colour as a mix of red, green, and blue light, with optional transparency. It also covers hex notation.

On this page

Example

#
CSS
.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);
}
Preview

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

#
Syntax
rgb(<red> <green> <blue> [/ <alpha>]?)
#rrggbb  #rrggbbaa  #rgb  #rgba

Syntax variants

#
  • rgb(124 58 237)

    purple, full opacity

    Three numbers from 0 to 255: a fair amount of red, little green, and the most blue.
  • rgb(124 58 237 / 50%)

    the same purple, half opacity

    Alpha after a slash. The checkerboard shows through the colour, while the text stays fully visible.
  • rgb(100% 60% 0%)

    orange in percentages

    Components in percentages instead of numbers. 100% is 255 and 60% is 153, giving the colour 255, 153, 0.
  • #10b981

    emerald in hex

    Hexadecimal notation: 10 of red, b9 of green, and 81 of blue, which is rgb(16 185 129).
  • #f43

    shorthand for #ff4433

    Three digits are a shorthand in which each digit is doubled: #f43 means #ff4433.
  • #0ea5e980

    hex with alpha

    The fourth pair of digits is alpha: 80 in hexadecimal is 128 out of 255, which is about 50% opacity.
  • rgba(14, 165, 233, 0.6)

    older syntax

    The former notation with commas and the letter β€œa” in the name. It still works, but in new code rgb() with a slash is enough.

Syntax variants in practice

#

rgb(124 58 237)

Three numbers from 0 to 255: a fair amount of red, little green, and the most blue.
.demo { background: rgb(124 58 237); }

rgb(124 58 237 / 50%)

Alpha after a slash. The checkerboard shows through the colour, while the text stays fully visible.
.demo { background: rgb(124 58 237 / 50%); }

rgb(100% 60% 0%)

Components in percentages instead of numbers. 100% is 255 and 60% is 153, giving the colour 255, 153, 0.
.demo { background: rgb(100% 60% 0%); }

#10b981

Hexadecimal notation: 10 of red, b9 of green, and 81 of blue, which is rgb(16 185 129).
.demo { background: #10b981; }

#f43

Three digits are a shorthand in which each digit is doubled: #f43 means #ff4433.
.demo { background: #f43; }

#0ea5e980

The fourth pair of digits is alpha: 80 in hexadecimal is 128 out of 255, which is about 50% opacity.
.demo { background: #0ea5e980; }

rgba(14, 165, 233, 0.6)

The former notation with commas and the letter β€œa” in the name. It still works, but in new code rgb() with a slash is enough.
.demo { background: rgba(14, 165, 233, 0.6); }

More examples

#
Components in a variable, transparency at the point of use
CSS
: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));
}
Preview

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. The opacity property 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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.