sodcalculator

10 Tech How-To

CSS Color Contrast: How to Improve Readability

Guide 10-40306 · Updated · Step by step

Improve CSS color contrast by choosing text and background colors that meet accessibility contrast targets, then checking them with a contrast checker in the actual interface. For normal text, aim for a contrast ratio of at least 4.5:1; for large text and meaningful interface graphics, 3:1 is the usual minimum target under WCAG guidance.

What contrast means in CSS

CSS does not have a single property that makes text more legible by itself. Contrast comes from the relative luminance of foreground and background colors: a light color against a dark one generally contrasts more than two similar shades. A ratio of 1:1 means the colors have equal luminance; 21:1 is the maximum, represented by white against black.

The ratio is calculated from each color’s relative luminance, not by comparing the apparent difference between its RGB or hexadecimal values. A pair can look distinct on one display and still fail a contrast check. Conversely, hues that seem far apart may have similar luminance, making text difficult to read, especially for people with low vision or color-vision deficiencies.

CSS sets the relevant colors with properties such as color and background-color. For example, color: #222; background-color: #fff; gives dark text on a white background. The exact ratio should be measured with a reliable checker rather than estimated by eye.

Contrast targets to use

WCAG contrast criteria are common benchmarks for accessible web content. They distinguish text size and weight because larger text is generally easier to perceive, but simply making text bold does not remove the need to check it.

  • Normal text: aim for at least 4.5:1.
  • Large text: at least 3:1. WCAG defines large text as at least 18 point (about 24 CSS pixels) at regular weight, or at least 14 point (about 18.67 CSS pixels) when bold.
  • Meaningful non-text elements: use at least 3:1 contrast against adjacent colors for relevant visual information such as control boundaries and graphical objects. Decorative elements do not necessarily have the same requirement.
  • Enhanced contrast: 7:1 for normal text and 4.5:1 for large text are the higher AAA targets, where practical.

These are accessibility criteria, not a guarantee that every person will find a design comfortable. They also do not mean every visible color difference must meet 3:1: the requirement depends on whether the element conveys information or is needed to identify a control.

How to improve contrast step by step

  1. Identify the foreground and its real background. Check text, icons, placeholders, links, button labels, borders, and focus indicators. Include overlays, gradients, translucent backgrounds, and images where they appear behind content.
  2. Measure the current pair. Use a browser accessibility inspector, a design-tool contrast checker, or a trusted contrast-ratio tool. Enter the actual foreground and background colors, including opacity effects where supported.
  3. Change luminance before changing hue. Darken the text or lighten its background, or do the reverse. For example, replace pale gray text on white with a darker gray. Check that the adjusted color still fits the visual design.
  4. Test every state. Check default, hover, focus, active, disabled, selected, and error states where they communicate information. A link that passes normally may fail after a hover color change; a keyboard focus outline also needs to remain visible against nearby colors.
  5. Retest in context. Evaluate the page at different zoom levels and on varied screens, and inspect responsive layouts. If text lies over an image, a solid or sufficiently opaque backing can make its background more predictable.

For a small design system, define reusable color tokens and check the combinations the interface actually uses. For example, a dark-text-on-light-surface token can be tested once and applied consistently, while alert colors may need separate foreground and background pairings.

Using CSS without hiding the problem

Set readable base colors explicitly and avoid relying on browser defaults for important content. A simple pattern might be:

body { color: #222; background-color: #fff; }
a { color: #0645ad; }
a:focus-visible { outline: 3px solid #222; outline-offset: 2px; }

This is only an illustration: test each actual pairing, including the outline against the colors around it. A focus indicator should be clearly visible, and keyboard users should not lose it when navigating the page. Do not remove outlines without providing an effective replacement.

Opacity can make contrast harder to predict. A declaration such as color: rgba(0, 0, 0, .5) blends the text with whatever is behind it; its final color depends on that background. Similarly, gradients and images can create several local backgrounds under one text element. Test the rendered result or use a stable background treatment rather than checking only the source color value.

Common contrast mistakes

  • Trusting visual judgment alone: ambient light, display settings, and individual vision change how colors appear. Measure the pair.
  • Checking only body text: small labels, form hints, icons, charts, and controls may be harder to read than headings.
  • Using color as the only signal: do not distinguish errors, links, or chart categories by color alone. Add text, underlines, shapes, or another clear visual cue.
  • Assuming a color name or hue ensures contrast: “blue” and “yellow” do not describe luminance precisely enough to establish a passing ratio.
  • Forgetting transparency and background changes: a color that passes on white may fail on a tinted panel or image.
  • Making text larger instead of checking it: size may change which target applies, but it does not make every color combination readable.

Contrast is one part of accessibility, not the whole test. Readable font sizing, spacing, clear labels, keyboard operation, and meaningful focus states matter too.

Choosing a contrast checker

A useful checker reports the ratio and whether the pair meets the relevant text or non-text target. Browser developer tools are convenient for inspecting rendered pages; design tools can help during mockups. Whichever tool you use, verify that it handles alpha transparency and the actual background correctly. For text on an image or gradient, a single foreground/background pair may not describe every point behind the text, so inspect the weakest-contrast areas.

Automated checks can catch many color-pair problems but cannot judge every visual context. They may not know whether an icon is decorative, whether a nearby label explains a control, or whether a focus indicator is easy to locate. Combine automated checks with manual review and, when possible, feedback from users with different access needs.

Frequently Asked Questions

What is a good CSS color contrast ratio?
For most text, use at least 4.5:1. Large text can use 3:1 under WCAG contrast criteria. Higher ratios can improve legibility, but test the full interface rather than relying on one number alone.

How do I calculate contrast between two CSS colors?
Use a contrast checker that calculates relative luminance from the rendered foreground and background colors. Comparing hexadecimal digits or RGB channel differences by eye is not a reliable substitute.

Does changing opacity affect contrast?
Yes. Transparency blends a color with the content behind it, changing the rendered color and therefore the ratio. Check the composited result on every background where it appears.

Do links need more than color to stand out?
Color should not be the only way a link is identified. Underlining or another persistent visual cue helps distinguish links from surrounding text, including for people who cannot easily perceive the color difference.

Does dark mode automatically have accessible contrast?
No. Dark mode needs its own checks. Dim text on a dark surface can fail just as pale text on a light surface can, and colors may change across themes or states.

Is contrast enough to make a website accessible?
No. It addresses a specific barrier. Also check keyboard access, focus visibility, text resizing, labels, structure, and whether information is communicated without color alone.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →