Accessible CSS

Intermediate · 14 min

How CSS decides whether people can use a page: visible focus, forced-colors mode, reduced motion, scalable text, target size and hiding content properly.

Before thisDebugging CSS with DevToolsPseudo-classes and Pseudo-elementsUser Preference Queries and Dark Mode

Kevin Powell · 14 min

Accessible CSS

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Never remove the focus indicator without replacing it. Use `:focus-visible` so keyboard users get a clear ring while mouse clicks on buttons do not.
  • If you must hide an outline, set `outline-color: transparent` rather than `outline: none`. In forced-colors (high contrast) mode the browser repaints a transparent outline as a visible one, while `none` stays invisible.
  • `@media (prefers-reduced-motion: reduce)` is a request from people who get dizzy or distracted by movement. Remove large, parallax and looping animation under it, and keep essential feedback.
  • Size text in `rem` and let the layout flex, so browser zoom and larger default font sizes work. Do not use `px` for font sizes or `user-scalable=no`.
  • Hiding has meanings. `display: none` and `visibility: hidden` hide from everyone including screen readers. A visually-hidden class hides only the visuals.

Where CSS matters for accessibility

Accessibility is mostly about structure, which is HTML’s job. The frontend roadmap’s accessible HTML and ARIA basics lesson covers semantics, labels and roles. CSS cannot fix a page built from <div>s, but it can break a good one. Here are the places where styling decisions determine whether people can use the page.

Focus you can see

Keyboard users move through links and controls with Tab, and the focus indicator is their cursor. Removing it is one of the most common accessibility bugs:

/* Do not */
button:focus { outline: none; }

/* Do */
:focus-visible {
  outline: 3px solid royalblue;
  outline-offset: 2px;
}

:focus-visible matches when the browser decides a visible indicator is helpful: always for keyboard focus, and generally not for a mouse click on a button. That settles the old argument about the ring looking ugly after a click. It has been in all major browsers since 2022 (Chrome 86, Firefox 85, Safari 15.4 per MDN’s data).

The indicator needs enough contrast against both the element and the background, and should not be clipped by overflow: hidden on a parent, which is why outline-offset and padding matter. Pseudo-classes and pseudo-elements has :focus, :focus-within and :focus-visible side by side.

Outlines and forced colours

Some people use forced-colors mode (Windows High Contrast and similar), where the browser replaces your colours with a small system palette. It drops box-shadow and background images, so a focus style made from those vanishes. Outlines survive, and a transparent outline gets painted in a system colour. The video’s tip is to use:

.card:focus-visible {
  outline: 2px solid transparent;      /* shows in forced colours */
  box-shadow: 0 0 0 3px royalblue;     /* your design, ignored there */
}

You can test forced colours and the other preferences without changing system settings via the Rendering tab, described in debugging CSS with DevTools. Both forced-colors and the prefers- family are media queries, from the same family as dark mode.

Motion

Movement across the page, parallax and endless loops can trigger dizziness and nausea for people with vestibular conditions, and distract people with attention difficulties. Operating systems have a “reduce motion” setting and browsers expose it:

.hero { animation: drift 20s linear infinite; }

@media (prefers-reduced-motion: reduce) {
  .hero { animation: none; }
}

Or the other way around, which is safer because motion is then opt-in:

@media (prefers-reduced-motion: no-preference) {
  .hero { animation: drift 20s linear infinite; }
}

Reduced does not mean zero. A short colour change on hover is fine, and a button that confirms an action may still need a visible change. Remove the large and the continuous. The transitions and scroll-driven animations lessons showed where to apply it, and scroll-behavior: smooth from scroll snap and smooth scrolling belongs under the same media query.

Text and zoom

  • Use rem for font sizes, so people who set a larger default size in their browser get it. A font size in px ignores that setting.
  • Allow zoom. Do not set user-scalable=no or a low maximum-scale in the viewport meta tag.
  • Let boxes grow. Fixed heights clip text when it is enlarged. Prefer min-height and padding.
  • Line length and spacing: around 45 to 80 characters per line and a unitless line-height near 1.5 for body text. WCAG’s text spacing criterion expects layouts to survive larger letter and line spacing, which fixed-height boxes break.
  • Contrast: WCAG asks for 4.5:1 for normal text and 3:1 for large text and UI components. The colours lesson covers the colour side.

Target size

Small hit areas hurt people with motor impairments and everyone on a phone. WCAG 2.2 sets a minimum target size of 24 by 24 CSS pixels at AA, with larger recommended for touch. A small icon can have padding or a larger ::before hit area to reach it without growing visually.

Hiding things properly

Different CSS hides in different ways:

Rule Visible Screen readers
display: none no skipped
visibility: hidden no skipped
opacity: 0 no still announced, and still focusable
Visually hidden (below) no announced
.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

Use the last form for text that only assistive technology needs: an icon button’s label, or a “skip to content” link. Do not use opacity: 0 or left: -9999px tricks for content that should be hidden from everyone, since they leave it focusable.

Content from CSS

Text added with ::before and content is usually announced by screen readers, though behaviour varies. Do not put meaningful information only in generated content, and prefer decorative uses such as icons and quote marks.

What the video covers

Kevin Powell’s “3 super small changes to improve your CSS” (March 2023, about 14 minutes) has three chapters: outline-color: transparent (0:26), prefers-reduced-motion (3:49), and focus-visible (11:35). Its description links articles on vestibular issues with parallax design and on reduced motion. That covers the first three sections here. Everything on text size, target size and hiding comes from the documentation. I could not read a transcript. His quick guide to focus states is in further viewing.

Where this fits

This applies pseudo-classes and user preference queries to a purpose. Next, CSS performance looks at the other thing a page owes its visitors, speed.

Check yourself

  1. 01Why is `button:focus { outline: none }` a bug, and what is a better rule?

    It removes the only indicator showing keyboard users where they are. A better pattern is to keep the default or provide a clear replacement on `:focus-visible`, for example `:focus-visible { outline: 3px solid; outline-offset: 2px; }`. `:focus-visible` shows for keyboard navigation but usually not for mouse clicks on buttons.

  2. 02Why does the video recommend `outline-color: transparent` over `outline: none` when you restyle focus?

    In forced-colors mode (Windows High Contrast and similar), the browser overrides author colours and paints a transparent outline in a system colour, so the outline is still visible. `outline: none` removes the outline entirely, and if your replacement relies on `box-shadow` or a background, forced-colors mode removes those too.

  3. 03A hero has a parallax scroll effect and a looping background animation. What should you do for users who prefer reduced motion?

    Wrap the motion in `@media (prefers-reduced-motion: no-preference)` or disable it under `reduce`. Large movement, parallax and looping motion can trigger vestibular symptoms. Small, short transitions are usually fine, and motion that conveys information needs a non-motion alternative.

  4. 04What is the difference between `.sr-only` and `display: none` for a text label?

    `display: none` removes the element from the accessibility tree, so screen readers skip it. A visually-hidden class (clip the box to a pixel, no size, absolutely positioned) hides it on screen but keeps it in the tree, so screen readers announce it. Use the latter for labels like an icon button's text.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark