Skip to content
Tutorials Center
My learning

Topics

Lesson 114 of 139 ยท Best Practices

Accessible CSS

Write CSS that works for everyone, with readable contrast, clear focus styles, accessible hiding, reduced motion, zoom support and forced colors mode.

Accessibility is often thought of as an HTML job: the right elements, labels and alt text. Those matter a lot, and the HTML course covers them. But CSS can make or break accessibility just as easily. A pale gray text color can make an article unreadable. One line, outline: none, can leave keyboard users lost. display: none on the wrong element can hide content from screen readers that was meant only to be visually tucked away.

In this lesson you will learn the CSS side of accessibility: color contrast, focus indicators, hiding content correctly, respecting motion preferences, surviving zoom, and working with Windows forced colors mode. None of it is hard, and most of it makes the site better for everyone.

Color contrast

Text needs enough contrast against its background to be read by people with low vision, people on cheap screens and anyone outside in bright sunlight. The Web Content Accessibility Guidelines (WCAG) measure contrast as a ratio from 1:1 (no difference) to 21:1 (black on white). The common targets are:

  • 4.5:1 for normal body text.
  • 3:1 for large text (about 24px, or about 18.7px bold).
  • 3:1 for meaningful non-text parts: input borders, icons that carry meaning and focus indicators.

You don't need to calculate these by hand. Browser DevTools show the contrast ratio when you inspect a text color, usually with a pass or fail mark. Light gray placeholder-style text, such as #aaa on white, is the most common failure. It is only about 2.3:1.

Low and good contrast

Inspect any paragraph in DevTools and look for the contrast ratio next to its color. Notice the fix for the orange is not a different color, just a darker shade of the same one.

Also, never use color alone to carry meaning. A red border on an invalid field should come with an icon or an error message, because many people can't tell red from green.

Focus styles

People who use a keyboard, switch device or voice control move through a page by focus. The focus indicator is their mouse pointer. Removing it is like hiding the cursor.

The rule is simple: never remove the focus outline unless you replace it with something at least as visible. The :focus-visible pseudo-class makes that easy, because browsers apply it when the user is navigating with a keyboard, but usually not after a mouse click on a button:

CSS
:focus-visible {
  outline: 3px solid #1a5fd0;
  outline-offset: 3px;
}

Outlines are ideal for focus rings: they don't change layout, they follow border-radius, and outline-offset adds a gap so the ring stands out from the element.

Clear focus indicators

Press Tab and Shift+Tab. Each item gets a thick orange ring with a small gap. Then click a button with the mouse: in most browsers no ring appears, because :focus-visible knows a mouse user doesn't need it. Uncomment the bad rule and the ring disappears for keyboard users too.

Hiding content the right way

There are three different kinds of "hidden", and picking the wrong one is a common bug:

Goal CSS Seen? Read by screen readers?
Hide from everyone display: none or the hidden attribute No No
Hide visually, keep for screen readers a .visually-hidden class No Yes
Keep the space, hide the content visibility: hidden No (space stays) No

The middle row is the one you need for things like a "Search" label next to a magnifying glass icon, or extra context such as "(opens in a new tab)". Here is a well-tested pattern:

CSS
.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

It shrinks the element to a single clipped pixel, takes it out of the flow and keeps it in the accessibility tree. The !important flags are one of the few legitimate uses of !important: a utility like this must win against any component style.

Visually hidden labels

You see only an icon and a short link, but a screen reader announces "Search recipes", "Search button" and "Read more about winter soups". Remove the class from the label and it appears on screen.

Respecting motion preferences

Large movement, parallax and zooming animations can trigger dizziness and nausea for people with vestibular disorders. Operating systems offer a "reduce motion" setting, and CSS can read it with prefers-reduced-motion:

CSS
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

That blanket rule is a safe backstop. A more thoughtful approach is to replace large movement with gentle fades rather than removing every effect. Reduced motion means less motion, not no feedback. The Motion and Performance lesson goes deeper.

Zoom, text size and reflow

Many people zoom the page or raise their browser's default font size. Your CSS should cope with both:

  • Use rem for font sizes, not px. rem follows the user's font size setting; px ignores it. See Units.
  • Don't set fixed heights on text containers. When the text grows, it overflows a box with height: 80px. Use min-height or let content decide.
  • Support reflow. At 400% zoom, a 1280px window behaves like a 320px one. Content should reflow into a single column without horizontal scrolling, which a responsive layout gives you for free.
  • Never disable zoom. Leave user-scalable=no and maximum-scale=1 out of the viewport meta tag.

Spacing for text also matters. Some people use tools that increase line height, letter spacing and paragraph spacing. Layouts built with flexible sizes survive this; layouts with tight fixed boxes clip the text.

Forced colors mode

Windows has a contrast themes setting (formerly High Contrast mode). When it is on, the browser replaces your colors with a small palette the user picked: maybe yellow text on black. Background images and box shadows are removed. You can detect it with @media (forced-colors: active).

Most pages work fine, as long as you follow two rules:

  1. Don't rely on background colors or shadows alone for boundaries. A button made only of a colored background becomes floating text. A border or outline stays, because the browser recolors it rather than removing it. A common trick is a transparent border: invisible normally, visible in forced colors mode.
  2. Use system colors when you must style something for this mode: CanvasText, Canvas, LinkText, ButtonText, ButtonFace, Highlight and friends.
Surviving forced colors

In normal mode, the selected chip is dark green and the borders are invisible. In forced colors mode (you can emulate it in Chromium DevTools under Rendering โ†’ "Emulate CSS media feature forced-colors"), every chip keeps a visible border and the selected one is underlined and outlined in the user's highlight color.

A few more habits

  • Generated content is announced. Text in ::before and ::after is read by most screen readers, so keep decorative symbols out of content, or use the alternative text syntax: content: "โ˜…" / "";.
  • Keep visual order and DOM order in sync. Flexbox order and grid placement can move items visually while the Tab order follows the HTML. Users then see focus jump around.
  • Make targets big enough. Small icon buttons are hard to hit. Aim for at least 24ร—24 CSS pixels, and ideally around 44ร—44.
  • Test with your keyboard. Unplug the mouse for five minutes and use your site. It is the fastest accessibility check there is.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What contrast ratio does WCAG ask for normal-size body text?

    Normal text needs at least 4.5:1. Large text and meaningful UI parts need at least 3:1.
  2. 2. Which way of hiding keeps text available to screen readers?

    The visually hidden pattern removes the content from view but keeps it in the accessibility tree. The others hide it from everyone.
  3. 3. Why is :focus-visible usually better than :focus for focus rings?

    :focus-visible uses the browser's judgment about when a visible indicator is helpful.
  4. 4. In forced colors mode, why is a transparent border on a button useful?

    Forced colors mode replaces your colors, including transparent border colors, with the user's palette, so the boundary appears.

Summary

  • Aim for 4.5:1 contrast for body text and 3:1 for large text and UI parts; check in DevTools.
  • Never remove focus indicators without a stronger replacement; use :focus-visible with an outline.
  • Use a visually hidden class for screen-reader-only text; display: none hides from everyone.
  • Honor prefers-reduced-motion by toning motion down.
  • Size text in rem, avoid fixed heights and let layouts reflow at high zoom.
  • In forced colors mode, rely on borders and outlines, and use system colors when needed.

Next: Organizing CSS, where you will learn naming conventions and file structures that keep a growing stylesheet manageable.