CSS Colors
selector {
color: colorValue;
background-color: colorValue;
}
Named Colors
CSS understands a set of plain English color names, like red, blue, and white, that you can use directly as a value without any special formatting.
- Named colors are great for quick prototypes, but switch to hex or rgb once you need an exact brand color.
- Accepted values:
- named color — keywords like red, teal, tomato
- #rrggbb — hex code, e.g. #ff6600
- rgb(r, g, b) — channels from 0 to 255
- hsl(h, s%, l%) — hue, saturation, lightness
- rgba() / hsla() — adds an alpha (opacity) channel
- transparent — fully transparent
- currentcolor — uses the element's text color
Example: Named Colors
<style>
p {
color: blue;
background-color: white;
}
</style>
<p>Named colors example</p>
Hex Colors
A hex color is written as a hash followed by six characters representing red, green, and blue in pairs, like #1a73e8. It's the most common way to express an exact color in real projects.
- Design tools almost always export colors as hex codes, so learning to read them makes translating a design into CSS much faster.
- Accepted values:
- #rgb — 3-digit shorthand; each digit is doubled (#f60 = #ff6600)
- #rrggbb — 6-digit red, green, blue pairs, 00 to ff
- #rgba — 4-digit shorthand including alpha
- #rrggbbaa — 8-digit form; aa is alpha, 00 transparent to ff opaque
Example: Hex Colors
<style>
p {
color: #1a73e8;
}
</style>
<p>Hex color example</p>
RGB and RGBA
The rgb() function builds a color from three numbers between 0 and 255, for red, green, and blue. Adding a fourth value (rgba) controls opacity, from 0 (invisible) to 1 (fully solid).
- Use rgba() whenever you need a color that's partially see-through, like a subtle overlay on top of an image.
- 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: RGB and RGBA
<style>
p {
color: rgb(26, 115, 232);
background-color: rgba(0, 0, 0, 0.5);
}
</style>
<p>RGB and RGBA example</p>
HSL and HSLA
The hsl() function describes a color by hue (a position on the color wheel, 0 to 360), saturation (how vivid the color is), and lightness (how light or dark it is), which many people find more intuitive than rgb.
- HSL makes it easy to create matching color variations, just adjust the lightness value up or down to get lighter or darker shades of the same hue.
- Accepted values:
- hsl(h, s%, l%) — hue, saturation, lightness
- hue — an angle 0 to 360 (0 red, 120 green, 240 blue)
- saturation — 0% is gray, 100% is fully vivid
- lightness — 0% is black, 50% is the pure color, 100% is white
- hsl(h s% l% / a) — modern syntax with alpha
Example: HSL and HSLA
<style>
p {
color: hsl(210, 82%, 51%);
}
</style>
<p>HSL color example</p>
Choosing Accessible Color Contrast
Not every color combination is easy to read. Accessible design means picking enough contrast between text and its background so that visitors with low vision or color blindness can still read it comfortably.
Example: Choosing Accessible Color Contrast
<style>
.good-contrast {
color: #111111;
background-color: #ffffff;
}
.poor-contrast {
color: #eeeeee;
background-color: #ffffff;
}
</style>
<p class="good-contrast">Easy to read</p>
<p class="poor-contrast">Hard to read</p>
- Using a color name for a very specific brand color instead of an exact hex or rgb value.
- Forgetting that rgba() and hsla() need a fourth value between 0 and 1 for opacity.
- Picking colors with too little contrast between text and background, which hurts readability.
- CSS colors can be written as named colors, hex codes, rgb()/rgba(), or hsl()/hsla().
- Hex codes are the most common way to express exact brand colors in real projects.
- The a in rgba and hsla adds an alpha channel that controls transparency.
All standard CSS color formats covered here are supported by every modern browser.
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