Sub topic 4
Contrast: making elements stand out from their surroundings
Prerequisites and scope
This chapter assumes you know basic HTML (headings, links, buttons) and basic CSS (selectors, custom properties, the box model). It also assumes you read the previous chapters on size and the type scale. Here you will learn how contrast creates emphasis. The next chapter covers measuring contrast with WCAG ratios in detail, so this chapter only previews that idea.
What contrast means in design
Contrast is a noticeable difference between an element and what surrounds it. The eye is drawn to differences. A single dark word in a paragraph of gray text stands out. A single dark word in a paragraph of equally dark text does not.
This gives a useful rule: an element gets attention because it differs from its neighbors, not because of any absolute property. A bold heading stands out on a page of regular text. On a page where everything is bold, the same heading gets lost.
Contrast works on several properties. The ones you control most often in CSS are:
- Value: how light or dark a color is.
- Color: the hue and its saturation (how vivid it is).
- Weight: how thick the strokes of text or borders are.
- Style and shape: filled versus outlined, rounded versus square, solid versus dashed.
Size is also a form of contrast, and you have already seen it. This chapter focuses on the other four.
Value contrast: the strongest cue
Value is how light or dark a color is, ignoring its hue. It is the most reliable contrast cue because it works for almost everyone, including people with color vision deficiency and people viewing a screen in bright sunlight.
To see value on its own, imagine converting the page to grayscale. If an element still stands out, it has good value contrast. If it disappears, it relied on hue alone.
Value contrast is also what makes text readable. Body text needs a strong value difference from its background. Secondary text, such as captions and timestamps, can use a smaller difference. That lower value contrast signals lower importance, as long as it stays readable.
:root {
--text-primary: #1a1a1a;
--text-secondary: #5c5c5c;
--surface: #ffffff;
}
body {
background: var(--surface);
color: var(--text-primary);
}
.meta {
color: var(--text-secondary);
font-size: 0.875rem;
}Here the main text is near black on white, and the metadata is a mid-gray. The difference in value tells readers which text to read first.
Color contrast: using hue and saturation
Color can direct attention when most of the page is neutral. A page built from grays, whites, and one saturated blue will send the eye to the blue. That is why many interfaces use a single accent color for the main action.
Two points keep this working:
- Reserve the accent. If links, icons, badges, borders, and headings all use the accent, nothing is special about it. Use it only where you want attention.
- Do not depend on hue alone. About 1 in 12 men and 1 in 200 women have some form of color vision deficiency. A red error message and a green success message can look alike to them. Pair color with a second cue such as an icon, a label, or a different weight.
Saturation matters too. A vivid color on a muted background stands out. A vivid color on another vivid color causes visual vibration and fights for attention. Placing a bright orange button on a bright blue panel makes both harder to look at.
Weight contrast: thick versus thin
Weight is how heavy an element looks. In text, it is font-weight. In shapes, it is border thickness or whether a shape is filled.
Weight contrast works best when the difference is clear. A font-weight of 400 next to 500 is barely visible. A 400 next to 700 is obvious. If your font has a variable weight axis, you can choose, but keep the steps wide enough to be seen.
Many designers use a small set of weights, for example 400 for body text and 700 for headings and labels. A limited set keeps the weight differences meaningful.
Style contrast: filled versus outlined
Filled shapes carry more visual weight than outlined ones. A solid blue button draws the eye more than a button with only a blue outline. This lets you rank actions that sit next to each other.
The example below shows a primary and a secondary button. Both share the same color and size. The filled background and bold text make the primary action dominant.
.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;
}Notice that the two buttons use the same hue. The hierarchy comes from fill and weight. This is a good pattern because it keeps the palette small and the ranking obvious. A third, tertiary action can drop the border as well and look like a plain text link.
| Level | Treatment | Typical use |
|---|---|---|
| Primary | Filled background, bold text | The one main action in a view |
| Secondary | Outline, regular text | Alternative actions |
| Tertiary | Text only, no border | Low-priority actions such as "Cancel" |
Contrast has a budget
Contrast is relative, so every extra high-contrast element reduces the effect of the others. Think of it as a budget. If a page has one orange button on a gray page, the button wins. If it has six orange buttons, none wins.
Practical ways to stay within budget:
- Choose one primary element per view and give it the strongest treatment.
- Make supporting elements quieter, not louder. Lowering the contrast of secondary content is often more effective than raising the contrast of the main content.
- Check the page in grayscale and with the squint test (covered in the last chapter of this tutorial) to see which elements still dominate.
Contrast for readability versus contrast for emphasis
These two goals are related but not identical, and mixing them up is a common mistake.
- Readability contrast makes sure text can be read. It has a minimum. Text below that minimum fails for many users, whatever the designer intended.
- Emphasis contrast ranks elements. It is relative and flexible.
A secondary caption can be quieter than body text for emphasis, but it must still stay above the readability minimum. You lower emphasis by changing several cues together: slightly smaller size, lighter weight, and a moderately lower value difference. You do not make it unreadable.
WCAG, the Web Content Accessibility Guidelines, define the minimum as a contrast ratio, a number from 1 (no difference) to 21 (black on white). Level AA asks for at least 4.5:1 for normal text and 3:1 for large text and for UI components such as button borders and form field outlines. The next chapter explains how this number is calculated. Here is the function from that chapter, so you can check the examples in this one.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
function luminance([r, g, b]) {
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
console.log(contrastRatio([0, 0, 0], [255, 255, 255])); // 21Common errors
These mistakes appear again and again in real interfaces.
Low-contrast placeholder and disabled text
Light gray text on white looks clean in a mockup but is hard to read. Placeholder text in form fields is a frequent offender. Keep it readable, and never use it in place of a visible label.
Text over images
Text placed on a photo has a background that changes from point to point, so its contrast changes too. Add a solid or gradient overlay behind the text, or place the text in a panel with its own background. Test the lightest part of the image under the text.
Everything is emphasized
A page with bold text, colored text, badges, and highlighted boxes everywhere has no hierarchy. If you find yourself adding emphasis to fix a problem, first try removing emphasis from the other elements.
Relying on hue for state
Links that differ from body text only by color are a classic failure. Underline links, or give them a clear second cue. The same applies to selected tabs, active menu items, and chart series.
Ignoring dark mode
When you invert a palette, contrast relationships can break. A mid-gray that worked on white may be too dim on near black. Define colors as custom properties and test each theme separately.
:root {
--surface: #ffffff;
--text-primary: #1a1a1a;
--text-secondary: #5c5c5c;
}
@media (prefers-color-scheme: dark) {
:root {
--surface: #121212;
--text-primary: #f2f2f2;
--text-secondary: #b3b3b3;
}
}Note that the dark theme does not use pure white on pure black. Extreme contrast over a large area can cause glare for some readers. Slightly softened values keep the hierarchy while reducing strain.
How contrast fits with the other cues
Contrast alone seldom builds a full hierarchy. It works best with size and position. A large heading in a darker value at the top of a content area uses all three. The later chapter on combining cues covers how to avoid giving too many elements the same strong treatment. For now, remember two points:
- Contrast ranks elements by difference from their surroundings, so the surroundings matter as much as the element.
- Use the smallest set of contrast changes that produces a clear ranking.
Exercise
A page has a blue "Buy now" button and a blue "Add to wishlist" button next to each other. Both use the same blue background, white bold text, and the same size. Users cannot tell which action the page recommends. Describe two CSS changes that fix this using contrast, and explain why you would not make the primary button red to fix it.
Show answer
Keep the "Buy now" button filled with the blue background, white text, and bold weight. Change "Add to wishlist" to the secondary style: a transparent background, a 2px blue border, blue text, and regular font weight. This uses style contrast (filled versus outlined) and weight contrast (700 versus 400) so that the primary action stands out. Making the primary button red would add a second hue to a palette that works with one accent, and red often signals errors or danger, which sends the wrong message. It would also rely on hue alone, which does not help users with color vision deficiency, whereas fill and weight work for everyone.
Exercise
You use #767676 text on a white background for a caption. Using the contrastRatio function from this chapter, would you expect it to meet the WCAG AA minimum for normal text, and what would you do if it did not?
Show answer
Run contrastRatio([118, 118, 118], [255, 255, 255]). The result is about 4.54, which is just above the 4.5:1 minimum for normal text, so it passes. If a value had come out below 4.5, you would darken the text color (for example to #5c5c5c) until the ratio reached at least 4.5 while keeping it lighter than the body text, so the caption still reads as secondary.