Sub topic 4
Contrast: making elements stand out from their surroundings
This chapter assumes you know basic HTML (headings, links, buttons) and basic CSS (selectors, properties, the box model). It builds on the previous chapter, where size made some elements dominate. Contrast works differently. It makes an element differ from what surrounds it, so the eye lands on it first.
What contrast means for hierarchy
Contrast is a difference between an element and its context. An element draws attention because it differs from its neighbors, not because of any absolute property. A bold word stands out in a paragraph of regular text. In a paragraph where every word is bold, no word stands out.
You can create contrast along three main dimensions:
| Dimension | What differs | Example |
|---|---|---|
| Value | Light versus dark | Dark text on a pale background |
| Color | Hue and saturation | One saturated blue button on a neutral page |
| Weight | Thickness of strokes or fills | A solid filled button next to an outline button |
Value is the strongest of the three. Color can fail for readers with color vision deficiency, so never rely on hue alone to separate two things.
A worked example: primary and secondary actions
A form often has two actions, such as "Save" and "Cancel". You want "Save" to win. The following CSS gives both buttons the same size and shape. Only contrast separates them.
.button {
padding: 0.6rem 1.2rem;
border-radius: 4px;
font: inherit;
cursor: pointer;
}
.button--primary {
background: #0b57d0;
color: #ffffff;
border: 2px solid #0b57d0;
font-weight: 700;
}
.button--secondary {
background: transparent;
color: #0b57d0;
border: 2px solid #0b57d0;
font-weight: 400;
}The primary button is a solid block of saturated blue with bold white text. The secondary button has the same hue, but it is only an outline. Its visual mass is much smaller, so it recedes. Both still look like buttons, and both are readable.
Notice what was not changed: size, position, and hue. Sharing the hue keeps the two buttons visibly related. Varying the fill and weight sets their priority.
Contrast also has a floor
Contrast creates emphasis, but text also needs enough contrast to be read at all. The Web Content Accessibility Guidelines (WCAG) set minimum contrast ratios:
- 4.5:1 for normal body text.
- 3:1 for large text (about 24px, or about 18.7px if bold).
- 3:1 for UI components, such as the border of an input field or a button outline.
The ratio runs from 1:1 (identical colors) to 21:1 (black on white). The blue in the example, #0b57d0, has a contrast ratio of about 6.4:1 against white. That passes for both the white text on the filled button and the outline on the secondary button. The next chapter shows how to calculate these ratios.
Common errors
Low-contrast "subtle" text. Designers often set secondary text in a light gray to push it down the hierarchy. #999999 on white has a ratio of about 2.85:1, which fails. #767676 on white is about 4.54:1, which is close to the lightest gray that passes.
.meta {
color: #767676; /* about 4.54:1 on white, passes for normal text */
font-size: 0.875rem;
}You can still push text down the hierarchy with a smaller size and a lighter weight. Do not use a color that readers cannot see.
Emphasizing everything. If every card has a colored border, a shadow, and a bright heading, nothing is the focal point. Contrast is relative, so each added accent reduces the others. Reserve strong contrast for one element per view and let the rest sit in a quieter range.
Using color alone. An error message that differs from normal text only by being red fails for users who cannot tell red from other hues. Add a second cue, such as an icon, a bold label, or the word "Error".
Forgetting states. A button that passes in its default state may fail when hovered, disabled, or focused. Check each state, and make sure the focus indicator itself has at least 3:1 contrast against its surroundings.
Summary
Contrast makes an element differ from its surroundings through value, color, or weight. Value is the most reliable of the three, and color should never carry meaning alone. Keep every piece of text above the WCAG minimums, and reserve your strongest contrast for the single element you most want readers to see.
Exercise
A page has a "Buy now" button and a "Learn more" link, both in the same blue, both the same size, and both with a solid filled background. The designer wants "Buy now" to be clearly primary. Name two changes that use contrast, not size, to do this, and say what you would check afterward.
Show answer
Change "Learn more" to an outline or plain text link style (transparent background, colored border or text, lighter font weight), and keep "Buy now" as the solid filled, bold button. This uses weight and fill contrast to separate the two. Afterward, check that the text and the outline of "Learn more" still meet WCAG ratios (4.5:1 for its text, 3:1 for its border) and that hover, focus, and disabled states pass as well.