Lots of apps write a white title straight over a photo: a hotel's name over the pool, a film's title over its poster. On one photo the words can be read, on another they vanish into a bright sky. This lesson has two tools to fix that: gradients and colour modification.
A gradient is a smooth change from one colour to another. You name only a few colours, called colour stops, and the browser fills in every colour between them. Picture the dawn sky from Sikunir hill, near Mrs. Sri's homestay: deep blue at the top, orange at the horizon, and no line where the blue stops. In that sky, the deep blue and the orange are the colour stops. Colour modification is making variants of one base colour. It is like a painter adding white paint (a tint), black paint (a shade) or grey paint (a tone, a calmer colour) to the same tube.
Gradients in CSS: direction and colour stops
This code gives .banner, a wide box on the page, a gradient background from Mrs. Sri's green #15803d to a darker green:
.banner {
background: linear-gradient(135deg, #15803d, #10602d);
}
linear-gradient(direction, colour, colour)draws a straight gradient as the element'sbackground. Without a direction it runs downwards (to bottom, the same as180deg).- Angles are written in degrees (
deg) and measured clockwise from the top:0degup,90degright,180degdown, and135degdown to the right. The 135deg diagonal is the direction most often used for decorative backgrounds. That's a habit, not a law. - On a square box,
to bottom rightequals135deg. On a wide box,to bottom rightis recalculated so the top right and bottom left corners get the middle colour, so the angle is no longer 135deg.
A colour stop can take a position as a percentage. In a gradient that runs downwards, the percentage is of the box's height. Two colours at the same position make a hard edge instead of a smooth change. That's how the Terrace Room photo is drawn in CSS: sky from blue #2563eb to orange #f59e0b down to 90% of the photo's height, then dark hills in the bottom 10%.
.photo { background: linear-gradient(#2563eb, #f59e0b 90%, #3f6212 90%, #1a2e05); }
Tint, shade and muted with HSL
As in the colour picking lesson, HSL writes a colour as three numbers. Hue is where the colour sits on the colour wheel (0 to 360 degrees), saturation is how colourful it is, and lightness is how light it is. Mrs. Sri's green, #15803d, is about hsl(142 72% 29%). From this one colour you can make three variants:
| Variant | What changes | Result | Used for |
|---|---|---|---|
| Tint (lighter) | lightness up to 95% | hsl(142 72% 95%) = #e9fbf0 |
the background of the "Free cancellation..." note |
| Shade (darker) | lightness down to 22% | hsl(142 72% 22%) = #10602d |
the book button on hover |
| Muted (calmer) | saturation down to 25% | hsl(142 25% 29%) = #375c45 |
a garden icon that shouldn't steal attention |
Lowering saturation for a calmer colour is called muted (or desaturate), the same as a painter's tone. The hue stays, so all three still feel like one family.
There is one thing the HSL numbers don't show: their lightness isn't the lightness the eye sees. hsl(60 100% 50%) (yellow) and hsl(240 100% 50%) (blue) both have 50% lightness, but the yellow's contrast against white is 1.07:1 and the blue's is 8.59:1. Muting changes contrast too, even though lightness stays: #15803d is 5.02:1 against white, #375c45 7.56:1. So after modifying a colour, measure its contrast again. Green #15803d text on the 95% tint is still 4.66:1, but on a 90% tint (#d3f8e1) only 4.37:1.
Scrim: white text over a photo
On the Terrace Room card, the room name and price are written in white right over the photo. The trouble is that a photo has light parts (the sky near the horizon) and dark parts (the hills). The fix is a scrim: a dark, see-through gradient over the photo. It is darkest behind the text and fades upwards, so the sky still shows.
.photo {
position: relative;
height: 240px;
background:
linear-gradient(to top, rgba(31, 41, 55, 0.8), rgba(31, 41, 55, 0) 60%), /* scrim */
linear-gradient(#2563eb, #f59e0b 90%, #3f6212 90%, #1a2e05); /* photo */
}
.photo .caption { position: absolute; left: 16px; bottom: 16px; color: white; }
.photo h2 { margin: 0 0 4px; font-size: 24px; font-weight: 700; line-height: 1.25; }
.photo p { margin: 0; font-size: 16px; line-height: 1.5; }
backgroundcan hold several layers, separated by commas. The first layer is on top, so the scrim is written before the photo.rgba(31, 41, 55, 0.8)is#1f2937written as numbers for red, green and blue, plus an alpha of 0.8. Alpha is how see-through a colour is: 1 is not see-through at all, 0 is fully see-through.to topstarts at the bottom:#1f2937at alpha 0.8 at the base, fading to alpha 0 at 60% of the photo's height (144px from the bottom).- Its end colour is
rgba(31, 41, 55, 0), the same colour at alpha 0, so only the transparency changes. position: absolutewithleftandbottomputs the text at the bottom left of the photo. That position is measured from.photo, because.photohasposition: relative. The room name takes the line from 166 to 196px from the top of the photo, the price 200 to 224px.
Measure at the lightest point
Text is hardest to read where the photo behind it is lightest. So the contrast of text over a photo is measured at the lightest point under the text, not the photo's average. The limits come from WCAG 1.4.3: normal text at least 4.5:1, large text (24px and up, or 18.66px bold) at least 3:1. The 24px bold room name is large text. The 16px price is normal text.
Now take a guess. You put a scrim over the bottom 30% of the photo, then keep darkening it up to 90% (alpha 0.9). Does the room name finally pass contrast?
| Scrim | Name (needs 3:1) | Price (needs 4.5:1) |
|---|---|---|
| No scrim | 2.39:1 at 196px, fails | 2.16:1 at 215px, fails |
| 30% tall, 90% dark | 2.79:1 at 168px, fails | 4.67:1, passes |
| 60% tall, 80% dark | 5.20:1 at 166px, passes | 6.61:1, passes |
The answer is in the second row: it doesn't pass. A 30% scrim stops 72px from the bottom, at 168px from the top, while the room name starts at 166px. So the top of the name sits over the orange sky, where the scrim has already faded to 0. Darkening the bottom to 90% never reaches that point. What helps is a taller scrim: at 60% tall and 80% dark, the lightest point under the name becomes #84683c, and its contrast 5.20:1.
Interpolation space: sRGB or oklch
The browser fills the colours between two stops (this is called interpolation) by computing in a colour space, which is a way of arranging all colours as numbers. A gradient written with hex or rgb() is computed in sRGB. In sRGB, the straight road from blue to orange passes through grey: the middle of the Terrace Room sky becomes #8d817b, a dull brown. Write in oklch, and the browser computes in oklch. This colour space doesn't pass through grey: the colour stays strong and the hue turns, so the middle becomes #df57a9, a pink like a real dawn sky.
.photo {
background: linear-gradient(#2563eb, #f59e0b 90%, #3f6212 90%, #1a2e05); /* fallback */
background: linear-gradient(in oklch, #2563eb, #f59e0b 90%, #3f6212 90%, #1a2e05);
}
- An old browser that doesn't know
in oklchdrops the secondbackgroundline and keeps the first one as a fallback. - For two colours with close hues (green to dark green), sRGB and oklch give almost the same result. The difference shows with hues far apart.
💡 Banding: stripes in a subtle gradient
An ordinary screen stores each red, green and blue channel in 256 levels. From #1f2937 to #111827, red drops only 14 levels, green 17, blue 16. So there are only 48 colours between them. Stretched over 800px, each colour gets about 17px, and it can show as stepped stripes (banding). Chrome and Safari hide it with dithering: two neighbouring colours are sprinkled pixel by pixel. Firefox, when we tested it, drew 48 flat stripes. To reduce banding: shorten the distance, widen the colour difference, or add a faint noise texture (tiny random dots).
⚠️ Text on a photo that still sinks
- Measuring contrast on the dark part of the photo. Measure at the lightest point under the text, because that's where the text is hardest to read.
- A scrim that's too short. Darkening the bottom doesn't help a line of text that sits above the scrim. Make the scrim tall enough to cover the whole text block.
- A gradient behind a paragraph. Contrast changes along the line, so every word has to pass. Put long text on a plain background.
- Trusting HSL lightness. The same lightness can mean very different contrast. Check tints, shades and muted colours again.
TL;DR: A gradient is a smooth change between colours, and linear-gradient(direction, colour, colour) draws one as a background. 135deg (down to the right, the same as to bottom right on a square box) is the most common diagonal for decorative backgrounds. Two colour stops (the colours you name) at the same position make a hard edge. From one HSL colour: tint = lightness up, shade = lightness down, muted = saturation down. HSL lightness isn't the lightness the eye sees, so measure contrast again. White text over a photo needs a scrim, a dark see-through gradient written as the first background layer. Its contrast is measured at the lightest point under the text: 4.5:1 for normal text, 3:1 for large text. A scrim that's too short doesn't help however dark it gets. The browser fills the colours between stops by computing in a colour space. A hex gradient is computed in the sRGB colour space and can turn dull in the middle. Writing in oklch keeps its colour alive. A gradient with a small colour difference over a long distance can show stripes (banding).
⚡ Direction and colour stops
Write:
.banner {
background: linear-gradient(135deg, #15803d, #10602d);
}
Meaning: A gradient is a smooth change between colours, here from the green #15803d to the darker green #10602d. Without a direction, a gradient runs downwards. 135deg is down to the right, the most common diagonal for decorative backgrounds. Two colour stops (the colours you name) at the same position, for example both at 90%, make a hard edge.
⚡ Tint, shade, muted
Meaning: From the green #15803d in HSL (hue, saturation, lightness): a tint raises the lightness (#e9fbf0), a shade lowers it (#10602d), muted lowers the saturation (#375c45). The hue stays, so they are one family. After modifying a colour, measure its contrast again.
⚡ A scrim must be tall enough
Write:
.photo {
background:
linear-gradient(to top, rgba(31, 41, 55, 0.8), rgba(31, 41, 55, 0) 60%), /* scrim */
linear-gradient(#2563eb, #f59e0b 90%, #3f6212 90%, #1a2e05); /* photo */
}
Guess first: a white room name is written over a photo. A scrim, a dark gradient over the photo, covers only the bottom 30% of the photo, then gets darkened up to 90%. Does the room name (large text, needs 3:1) finally pass?
No, only 2.79:1. The scrim stops 168px from the top, while the room name starts at 166px. A 60% scrim at 80% dark gives 5.20:1.
Meaning: background can hold several layers, and the first layer is on top. In this code the first layer is the scrim: 80% dark (the 0.8), fading out at 60% of the photo's height. Measure contrast at the lightest point under the text, not the photo's average.
⚡ in oklch so it doesn't go dull
Meaning: The browser fills the colours between stops by computing in a colour space. A gradient with hex colours is computed in the sRGB colour space. As a result, the middle of blue to orange becomes a dull brown #8d817b. With in oklch, which asks the browser to compute in the oklch colour space, the middle is a pink #df57a9, like a real dawn sky.