Sub topic 9
Contrast and Legibility
This chapter assumes you can set type in a design tool, know the difference between size, weight and typeface, and have read the earlier chapters on hierarchy, negative space and color. Here you will learn how to measure whether text can be read against its background, and how to fix it when it cannot.
Contrast is a difference, not a color
Contrast is the difference between the text and what sits behind it. Two kinds matter in a type-and-image layout.
- Luminance contrast is the difference in lightness. Dark gray on white and white on navy both have high luminance contrast.
- Hue contrast is the difference in color, such as red against green. Hue contrast alone is unreliable. Red and green text of equal lightness can look identical to a viewer with color-vision deficiency, and they blur together on a small or dim screen.
Legibility depends mostly on luminance. A quick check is to convert your layout to grayscale. If the text vanishes, the layout depends on hue and needs to change.
Measuring contrast with a ratio
The Web Content Accessibility Guidelines (WCAG) define a contrast ratio from 1:1 (no difference) to 21:1 (black on white). Design tools and browser developer tools can calculate it from two colors.
| Text type | Minimum ratio (AA) | Enhanced ratio (AAA) |
|---|---|---|
| Body text | 4.5:1 | 7:1 |
| Large text (about 24 px regular, or 18.66 px bold, and up) | 3:1 | 4.5:1 |
| Interface parts and meaningful graphics | 3:1 | none |
Treat AA as the floor for published work. Body text on a plain background should usually go well past it.
Why images make this harder
A flat background has one color, so one ratio describes it. A photograph has thousands of colors, and the ratio changes from one word to the next. A headline can pass over a dark sky and fail where it crosses a bright cloud.
Check the worst case, not the average. Sample the lightest area (for white text) or the darkest area (for dark text) that any letter touches. If that spot passes, the whole line passes.
The order in the diagram is deliberate. Moving the text costs the image nothing. A scrim alters the photograph. Changing the type changes the design voice. Start with the cheapest change.
Factors beyond the ratio
A passing ratio does not guarantee comfortable reading. These factors change how much contrast you need.
- Size and weight. Thin strokes lose contrast because anti-aliasing blends them with the background. A light-weight face at 14 px needs more contrast than a bold face at the same size.
- Texture behind the text. Foliage, fabric and crowds create busy edges that compete with letter shapes. Even if the average ratio passes, the busy detail makes reading tiring.
- Line length and leading. Cramped lines over a detailed image look dense. Generous leading gives the eye clear horizontal channels.
- Polarity. Light text on a dark background can look thinner and can bloom, which makes letters seem to glow at the edges. Increase weight slightly or open the letter spacing a little for reversed text.
- Display conditions. Screens in sunlight, low-quality projectors and printed ink on uncoated paper all reduce effective contrast. Aim for a margin above the minimum.
Techniques that raise contrast
Move or resize the text
Place text in a calm area of the image, as covered in the negative space chapter. If the photo offers no calm area, a larger and heavier headline can pass the 3:1 threshold where body text cannot.
Adjust the image
Darken, lighten or blur the photograph. A global exposure change affects the whole picture. A localized adjustment, such as a gradient mask in the area behind the text, protects the focal point. A slight blur on a background image reduces edge detail and helps text stand out, but keep it away from the main subject.
Add a backing shape
A solid or semi-transparent panel behind the text gives complete control. It works well for small text such as captions or legal lines. The cost is that the panel is visible and covers part of the image, so align it to the grid and give it proper padding.
Use a scrim
A scrim is a translucent layer between the image and the text. The next chapter covers scrims and gradients in detail. The key rule here is to make the scrim only as strong as the check requires, then measure again.
Use a shadow or outline sparingly
A soft shadow with a large blur radius and low opacity can lift text a little. Hard drop shadows and thick outlines look dated, and they distort letter shapes. Treat them as a repair for a small remaining problem, not as the main solution.
Checking in code
You can calculate the WCAG ratio yourself. This script follows the WCAG definition of relative luminance and works with any hex color.
def channel(c):
c = c / 255
return c / 12.92 if c <= 0.03928 else ((c + 0.055) / 1.055) ** 2.4
def luminance(hex_color):
h = hex_color.lstrip("#")
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
def contrast(fg, bg):
l1, l2 = sorted((luminance(fg), luminance(bg)), reverse=True)
return (l1 + 0.05) / (l2 + 0.05)
if __name__ == "__main__":
print(round(contrast("#FFFFFF", "#000000"), 2)) # 21.0
print(round(contrast("#FFFFFF", "#767676"), 2)) # about 4.54
print(round(contrast("#FFFFFF", "#999999"), 2)) # about 2.85The middle result shows why #767676 is often cited as the lightest gray that passes AA for body text on white. The last result fails.
To test text over a photograph, sample the pixel color at the worst point under the text and pass it as the background. If a scrim sits in between, use the blended color. For a scrim of color s at opacity a over a pixel p, each channel is a * s + (1 - a) * p.
Common errors
- Checking against the wrong pixel. Sampling the average or the darkest spot for white text hides the failure that readers will see.
- Testing only on your monitor. Check on a phone at low brightness and, for print, on a proof.
- Trusting the opacity slider. A scrim at 40% over a very bright area may still leave the ratio too low. Measure the result.
- Using color alone for emphasis. Pale yellow highlights on white fail for everyone, not just for users with color-vision deficiency.
- Ignoring text that is part of an image. Text baked into a flattened image cannot adapt to different screens, and screen readers cannot read it.
- Applying the headline threshold to body text. The 3:1 allowance is only for large text.
A worked example
You have a sunrise photo with a bright horizon band across the middle and want a white headline over the lower third. Sampling the lower third shows a worst-case pixel of #8A7F72, a mid-tone brown.
- Calculate the ratio of white against
#8A7F72. It comes out near 3.9:1. That passes for large text but fails for a 16 px subtitle. - Move the text left, away from the horizon band. The worst case improves slightly but the subtitle still fails.
- Add a bottom-up black gradient at 55% opacity at its strongest. The blended worst-case color is roughly
#3E3933, and the ratio rises above 10:1. - Re-check at the top of the gradient, where it fades out. The headline's first line must still pass there.
Step 4 is the one people skip. A gradient is weakest where it fades, so test the edge of the text block nearest the fade.
Exercise
White 16 px body text sits over a photo. The darkest area under the text has a background of #6B6B6B, and the lightest area under the text has a background of #B0B0B0. Which area should you test, and what should you do if the ratio there is 2.4:1?
Show answer
Test the lightest area (#B0B0B0), because white text is hardest to read against light backgrounds and the worst case decides whether every letter is readable. A ratio of 2.4:1 fails both the 4.5:1 body-text target and the 3:1 large-text target. First try moving the text to a darker, calmer area. If that is not possible, add a scrim or a backing panel behind the text, then measure again at the weakest point of the scrim. Making the text bolder helps legibility but does not change the ratio, so it cannot be the only fix.
Exercise
A colleague says a red headline on a green background is fine because the two colors look very different. What check would you run, and what would you expect to find?
Show answer
Convert the layout to grayscale or calculate the luminance contrast ratio. Red and green of similar lightness have nearly the same luminance, so the ratio will likely be far below 3:1 and the text will look flat in grayscale. It will also be hard to read for people with red-green color-vision deficiency. The fix is to change the lightness of one color, not its hue.