- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS RGB Colors
CSS Colors and Backgrounds
CSS RGB Colors
Define sRGB colors with modern rgb() syntax, percentages, channel values, and optional alpha transparency.
rgb(255, 0, 0) is red: full red, no green, no blue. Every RGB color is just three numbers saying how much of each light to mix.
How RGB works
Each of the three numbers goes from 0 (none of that color) to 255 (as much as possible). Mixing light is not like mixing paint: all three at 255 gives white, and all three at 0 gives black. RGB is the same color model as CSS HEX Colors, just written in normal numbers instead of hexadecimal. For all the formats side by side, see CSS Colors.
| RGB | You get |
|---|---|
| rgb(255, 0, 0) | Red |
| rgb(0, 255, 0) | Bright green |
| rgb(0, 0, 255) | Blue |
| rgb(255, 255, 0) | Yellow (red + green light) |
| rgb(128, 128, 128) | Grey |
| rgb(255, 255, 255) | White |
Syntax
css
selector {
color: rgb(red, green, blue);
}Example
css
p {
color: rgb(255, 0, 0);
}That paragraph turns red.
Where you can use it
| Property | What it colors | Example |
|---|---|---|
| color | Text | color: rgb(0, 0, 255); |
| background-color | The background | background-color: rgb(240, 240, 240); |
| border-color | The border | border-color: rgb(255, 0, 0); |
| outline-color | The focus outline | outline-color: rgb(0, 255, 0); |
Adding transparency
Add a fourth value for opacity, from 0 (invisible) to 1 (solid). You will see two ways of writing it:
Half-transparent red
css
/* older style, still works everywhere */
color: rgba(255, 0, 0, 0.5);
/* modern style: spaces, and a slash before the alpha */
color: rgb(255 0 0 / 50%);Both mean the same thing. Modern browsers accept the alpha in plain rgb() too, so rgba() is no longer needed, but you will still meet it in a lot of code.
Example
Complete CSS RGB Color Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS RGB Colors</title>
<style>
body {
background-color: rgb(240, 240, 240);
}
h1 {
color: rgb(0, 0, 255);
}
p {
color: rgb(0, 128, 0);
}
div {
border: 2px solid rgb(255, 0, 0);
padding: 10px;
}
</style>
</head>
<body>
<h1>RGB Color Example</h1>
<p>This paragraph uses RGB color.</p>
<div>This box has a red border using RGB.</div>
</body>
</html>The page gets a light grey background, a blue heading, green text and a box with a red border.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| rgb() | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Good to know
- Values above 255 are clamped to 255, so
rgb(300, 0, 0)is just red. - Do not mix the two styles:
rgb(255, 0, 0 / 50%)with commas and a slash is invalid, and the color is ignored. - RGB is easy for computers but hard to adjust by eye. To make a color a bit lighter, HSL is usually more convenient.
CSS RGB Colors exercises
rgb()
Make the title red with rgb(255, 0, 0).
The HTML your code works on
<h2 class="title">Sale</h2>
<p class="text">All shoes half price.</p>
<div class="box">Box</div>Result
