Colour is where a design comes alive. CSS has several ways to specify it, each useful in different situations, plus gradients for smooth transitions between colours. Knowing them — and how to keep colour accessible — is core visual craft.
Colour formats
| Format | Example | Good for |
|---|---|---|
Named | red, tomato | Quick prototyping |
Hex | #2563eb | The common default |
rgb() | rgb(37 99 235) | When you need alpha |
hsl() | hsl(217 91% 60%) | Tweaking lightness/hue by hand |
Transparency
background: rgb(37 99 235 / 0.1); /* 10% opaque blue (modern syntax) */background: hsl(217 91% 60% / 0.5);color: #2563eb80; /* 8-digit hex: last 2 = alpha */.overlay { opacity: 0.5; } /* fades the WHOLE element + children */
Note the difference: an alpha in the colour makes just that colour translucent; opacity fades the entire element, content and all.
Gradients
/* Linear — a direction and colour stops */background: linear-gradient(to right, #2563eb, #4f7cff);background: linear-gradient(135deg, #2563eb 0%, #1e3a8a 100%);/* Radial — from a centre outward */background: radial-gradient(circle, #4f7cff, transparent 70%);/* Repeating & conic exist too */background: conic-gradient(from 0deg, red, blue, red);
Accessible colour
Practice
Build a button with an hsl() background and a hover state that's just 10% darker (lower the lightness). Add a translucent overlay using rgb(… / 0.6), and a hero background with a linear-gradient. Verify your text/background contrast passes 4.5:1.
Key takeaways
- 01Specify colour as named, hex (#rrggbb), rgb() or hsl(); hsl is easiest to adjust by hand.
- 02Add alpha in the colour (rgb(… / 0.5)) to fade just that colour; opacity fades the whole element.
- 03Gradients: linear-gradient (direction + stops), radial-gradient, conic-gradient.
- 04Body text needs ≥ 4.5:1 contrast — check it, and never rely on colour alone for meaning.
