CSS RGB Colors
In this page:
selector {
color: rgb(red, green, blue);
color: rgba(red, green, blue, alpha);
}
The rgb() Function
rgb(red, green, blue) takes three numbers from 0 to 255 describing how much of each light channel to mix -- rgb(255, 0, 0) is pure red, rgb(0, 0, 0) is black, and rgb(255, 255, 255) is white.
Modern CSS also accepts percentages or a space-separated syntax like rgb(255 0 0), both equivalent to the comma form.
- Accepted values:
- rgb(r, g, b) — each channel is 0 to 255 or 0% to 100%
- rgb(r g b / a) — modern syntax with alpha
- rgba(r, g, b, a) — alpha from 0 to 1 or 0% to 100%
- rgb(255 0 0) — pure red
- rgb(0 0 0) — black, rgb(255 255 255) is white
Example: The rgb() Function
<style>
.red { background: rgb(255, 0, 0); }
.black { background: rgb(0, 0, 0); }
.white { background: rgb(255, 255, 255); }
</style>
<div class="red">Pure red</div>
<div class="black">Black</div>
<div class="white">White</div>
Mixing Colors with RGB
Because RGB is additive light mixing rather than paint mixing, combining channels behaves differently than intuition from art class -- red and green light together make yellow, not brown.
Understanding the additive model helps predict what a given rgb() triple will actually look like before testing it.
- Accepted values:
- rgb(r, g, b) — each channel 0 to 255
- rgb(r g b) — modern space-separated syntax
- rgb(r g b / a) — adds alpha after a slash
- rgb(0% 50% 100%) — channels as percentages
- rgba(r, g, b, a) — legacy alias with alpha from 0 to 1
Example: Mixing Colors with RGB
<style>
.mix {
background: rgb(255, 255, 0);
}
</style>
<div class="mix">Red light + green light = yellow, not brown</div>
Adding Transparency with rgba()
rgba(red, green, blue, alpha) adds a fourth value from 0 (fully transparent) to 1 (fully opaque) controlling how much of whatever is behind the element shows through.
This is the standard way to create semi-transparent overlays, tinted backgrounds, or fade effects without a separate opacity property affecting the whole element.
- Accepted values:
- rgba(r, g, b, a) — alpha from 0 (transparent) to 1 (opaque)
- rgb(r g b / 50%) — alpha written as a percentage
- hsla(h, s%, l%, a) — HSL color with alpha
- #rrggbbaa — 8-digit hex with alpha
- transparent — shorthand for fully transparent black
Example: Adding Transparency with rgba()
<style>
.overlay {
background: rgba(0, 0, 0, 0.5);
color: white;
}
</style>
<div class="overlay">Semi-transparent overlay</div>
RGB vs Other Color Formats
RGB values map directly to hex color codes (each channel becomes two hex digits), so rgb(255, 0, 0) and #FF0000 are the exact same color -- the choice between them is purely about which is easier to read or write in context.
RGB's explicit numeric channels make it especially convenient when a color needs to be calculated or interpolated in JavaScript.
Example: RGB vs Other Color Formats
<style>
.rgb-red { background: rgb(255, 0, 0); }
.hex-red { background: #FF0000; }
</style>
<div class="rgb-red">rgb(255, 0, 0)</div>
<div class="hex-red">#FF0000 — the same color</div>
Practical RGB Patterns
A common pattern is defining a base color once and reusing rgba() with different alpha values for hover states, disabled states, or layered shadows, keeping the underlying color consistent while only varying opacity.
This avoids maintaining several near-duplicate hex colors for what is conceptually one color at different strengths.
Example: Practical RGB Patterns
<style>
.btn {
background: rgba(37, 99, 235, 1);
}
.btn:hover {
background: rgba(37, 99, 235, 0.8);
}
</style>
<button class="btn">Hover me</button>
- Writing
rgb(256, 0, 0)or values above 255, which the browser clamps to 255 instead of showing an error. - Using
rgba()with an opacity like50, when the alpha value is between 0 and 1 (or50%). - Mixing up channel order, so
rgb(0, 0, 255)is blue, not red.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- CSS Colors
- CSS RGB Colors
- CSS HEX Colors
- CSS HSL Colors
- CSS Color Keywords
- CSS Backgrounds
- CSS Background Color
- CSS Background Image
- CSS Background Repeat
- CSS Background Attachment
- CSS Background Shorthand
- CSS Multiple Backgrounds
- CSS Background Size
- CSS Background Origin
- CSS Background Clip
- CSS Borders
- CSS Border Style
- CSS Border Width
- CSS Border Color
- CSS Border Sides
- CSS Border Shorthand