Skip to main content

CSS Accessibility

Accessible CSS

Written by Updated Accessible CSS exercises

Preserve focus, readable contrast, zoom behavior, target size, and semantic reading order while styling interfaces. It addresses focus visibility, contrast, zoom and text resizing, target size, source order, hidden content, and the boundary between CSS and semantic HTML.

CSS can make a perfectly good page unusable: remove the focus outline, lock the text size, make buttons too small to tap, or reorder content so the keyboard jumps around. Accessible CSS is mostly about not breaking what the HTML already gives you.

The usual culprits are removed outlines, text that cannot be resized, tiny tap targets, and Grid or Flexbox moving things away from their reading order. The focus ring is covered in CSS Outline, and form states in Modern Form Styling.

Keep focus visible

Example

css

:focus-visible {
  outline: 3px solid #0ea5e9;
  outline-offset: 3px;
}

:focus:not(:focus-visible) {
  outline: none;
}

:focus-visible shows a clear ring when someone uses the keyboard, and the second rule hides it only after a mouse click, which is what most designers actually want. Never write outline: none on its own; if you remove the default, put something equally visible in its place.

Let text grow

Example

css

body {
  font-size: 1rem;
  line-height: 1.5;
}

.content {
  max-width: 70ch;
}

Font sizes in rem follow the reader's browser setting, and a line-height of 1.5 keeps lines readable. Do not fix heights on boxes with text in them: when someone zooms to 200%, the text needs room to grow.

  • Keep essential text contrast at least at the applicable WCAG threshold.
  • Do not communicate status by color alone.
  • Use source order that remains logical when Grid or Flexbox styling is removed.
  • Give pointer targets enough size and separation.
  • Test at 200% browser zoom and with increased text spacing.
  • Never hide focusable content with clipping, opacity, or off-screen positioning.

The checklist

CSS concernRequired outcomeUseful syntax
Keyboard focusThe focused control remains clearly visible.:focus-visible
Forced colorsSystem colors and controls remain distinguishable.@media (forced-colors: active)
Reduced motionNon-essential movement can be removed.prefers-reduced-motion
Zoom and reflowContent remains usable without two-axis scrolling.Relative units and flexible layout

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Accessible CSS example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

:focus-visible {
  outline: 3px solid #0ea5e9;
  outline-offset: 3px;
}

:focus:not(:focus-visible) {
  outline: none;
}

body {
  font-size: 1rem;
  line-height: 1.5;
}

.content {
  max-width: 70ch;
}
  </style>
</head>
<body>
  <main class="content"><h1>Keyboard focus</h1><p><a href="#">Focus this link</a></p><button>Focus this button</button><label>Name <input></label><p>Use Tab to inspect the focus indicator.</p></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
:focus-visible86868515.4
forced-colors media query89798916

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Test it

  • Unplug the mouse and use the page with Tab, Shift + Tab, Enter and Space only.
  • Zoom to 200% and check that nothing is cut off or overlapping.
  • Turn on Windows high contrast or forced colors and check that buttons still have visible edges.

CSS can only do so much. Missing labels, wrong headings and broken keyboard behavior must be fixed in the HTML and JavaScript.

Accessible CSS exercises

A visible focus

Give the button an outline of 2px solid blue when it gets focus from the keyboard, with :focus-visible.

The HTML your code works on
<button class="btn">Save</button>
<p class="text">Read the <a class="link" href="#">guide</a> first.</p>
<p class="error">Wrong email</p>

Result

Check your answer to see it here.