Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Functions

The rgb() function

Reference for the CSS rgb() function: modern and legacy syntax, percentages, alpha, the none keyword and relative colors, with live examples.

The rgb() function creates a color from three red, green and blue channel values in the sRGB color space, with an optional alpha (opacity) value. It is the functional form of the hex notation: rgb(255 0 0) and #ff0000 are the same red.

rgba() is an alias with identical behavior. Modern CSS lets rgb() take an alpha too, so you never need rgba() in new code.

CSS
.badge {
  background: rgb(37 99 235);
  border-color: rgb(0 0 0 / 25%);
}

Try it

Mixing channels and alpha

The last swatch uses a relative color: it starts from another color and rewrites its channels. If your browser is older and ignores that line, the swatch simply shows no background.

Syntax

CSS
/* modern, space-separated */
rgb(R G B)
rgb(R G B / A)

/* legacy, comma-separated (still valid) */
rgb(R, G, B)
rgb(R, G, B, A)

/* relative color */
rgb(from <color> R G B / A)

The result is a <color>. It is accepted anywhere a color is, for example color and background-color. Colors interpolate smoothly, so properties using them can be animated.

Syntax and arguments

Argument Description
R, G, B A number from 0 to 255, or a percentage from 0% to 100%. Values outside the range are clamped.
A Optional alpha: a number from 0 (transparent) to 1 (opaque) or a percentage. Default is fully opaque.
none Modern syntax only. Marks a channel as missing; it is treated as 0 when drawn but can be filled in when interpolating.
from <color> Relative color syntax. Inside the function, the keywords r, g, b and alpha hold the origin color's channels, and you can use them in calc().

Usage notes

  • Pick one syntax per project. The modern space-separated form with / for alpha is easier to read and supports none and relative colors; the legacy comma form cannot mix numbers and percentages.
  • Alpha affects the whole color, not the element. rgb(0 0 0 / 50%) on a background does not make the text transparent. That is what opacity does.
  • Channels are not perceptually even. Adding the same amount to each channel does not give a predictable lightness change. For palettes, hsl() or oklch() are easier to reason about.
  • Limited to sRGB. You cannot express the wider gamut of modern displays with rgb(); use color() or oklch().
  • Contrast. Whatever notation you use, check text contrast against its background.
  • Hex equivalence. #rrggbbaa is the same as rgb(r g b / a) with the channels written in hexadecimal.

Browser support

The rgb() function works in every browser; the space-separated syntax and relative colors are newer, so check a compatibility table if you must support older browsers.