Lesson 7 of 50
cssColors & Color Functions
8 mins
Course progress14%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 7 of 50
css8 mins
Keep these ideas in mind as you work through the lesson.
CSS offers a toolbox of color formats — each useful for different situations.
| Format | Example | When to use |
|---|---|---|
| Hex | #ff6600 | Quick, familiar, works everywhere. |
| RGB | rgb(255,102,0) | When you need integer values; good for JS interop. |
| RGBA | rgba(255,102,0,0.8) | Add opacity without extra opacity property. |
| HSL | hsl(30,100%,50%) | Hue‑saturation‑lightness lets you tweak hues easily. |
| HSLA | hsla(30,100%,50%,0.7) | Same as HSL but with opacity. |
currentColor | border:1px solid currentColor; | Inherit the element’s defined color. |
| CSS 4 color functions | color(), lab(), lch(), color-mod() (experimental) | High‑precision color spaces, useful for UI design systems. |
:root {
--hue-primary: 210; /* Cool blue */
--sat-primary: 60%;
--light-primary: 50%;
--primary: hsl(var(--hue-primary), var(--sat-primary), var(--light-primary));
--primary-dark: hsl(var(--hue-primary), var(--sat-primary), 30%);
}
body { color: var(--primary); }
@media (prefers-color-scheme: dark) {
body { color: var(--primary-dark); }
}
By storing hue and saturation separately you can change the entire brand colour with a single line.
Use semantic custom‑property names (--text-primary, --bg-surface) instead of raw colour values. This decouples your design tokens from the actual colour and makes theme switching trivial.