Sub topic 15
Accessibility: focus indicators and generated content
This chapter assumes you know CSS rule syntax, the difference between pseudo-classes and pseudo-elements, and the focus pseudo-classes :focus, :focus-visible, and :focus-within. It covers three accessibility problems: removed focus indicators, generated content that assistive technology reads unpredictably, and state shown by color alone.
Never remove focus without a replacement
Keyboard users rely on the focus indicator to see where they are on the page. A common mistake is a reset rule like this:
:focus {
outline: none;
}This leaves keyboard users with no indication of the focused element. If you remove the default outline, you must supply a visible one in the same change.
:focus-visible lets you keep a strong ring for keyboard users while avoiding a ring on mouse clicks, where the browser decides one isn't needed.
button:focus-visible,
a:focus-visible,
input:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}Use outline rather than border or box-shadow alone. Outlines don't change layout, and they remain visible in Windows High Contrast (forced colors) mode, where shadows are removed.
Generated content and screen readers
Text added with content in ::before or ::after is usually included in the accessibility tree. Many screen readers announce it. Behavior varies between browsers and screen readers, so don't depend on it.
- Use generated content only for decoration, such as arrows, icons, and bars.
- Never put essential information in
content. Text in CSS can't be selected, translated reliably, or found with search. - If a decorative symbol might be read aloud, provide alternative text after a slash. Browsers that support it treat the part after the slash as the accessible text. An empty string hides the symbol.
.external::after {
content: " \2197" / "";
}Browsers that don't support the slash syntax ignore the whole declaration. To keep a fallback, write the plain declaration first and the slash version after it.
.external::after {
content: " \2197";
content: " \2197" / "";
}An external link marker is a case where the information matters. Decoration that hides meaning from some users is not decoration. Put "(opens in a new tab)" in the HTML, for example in visually hidden text, so everyone gets it.
Don't signal state with color alone
A red border for an invalid field is invisible to users with color vision deficiency. Add a second cue, such as an icon, text, or a change in border style.
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
border-style: dashed;
}
input:required:invalid:not(:placeholder-shown) + .hint::before {
content: "Error: " / "";
font-weight: bold;
}The visual cue here is not enough on its own. Also connect the message to the field in HTML with aria-describedby, so assistive technology announces it.
Checks to run
- Press Tab through the page. Is the focused element always visible?
- Turn on a screen reader and listen to links and buttons that use
::beforeor::after. - View the page in grayscale. Can you still tell which items are active, invalid, or selected?
- Check
::selectionand::placeholdercolors for contrast. A pale placeholder, such as#aaaon white, is hard to read. The#767676used earlier is the lightest gray that meets 4.5:1 on white.
Exercise
A stylesheet contains a:focus { outline: none; } and a rule that changes link color on :hover only. Name the problem and write a fix.
Show answer
Keyboard users get no visible focus indicator, and the hover color never appears for them. Remove the outline: none rule, or replace it with a:focus-visible { outline: 3px solid #1a73e8; outline-offset: 2px; }. Apply the hover style to :focus-visible too, for example a:hover, a:focus-visible { text-decoration: underline; }.
Exercise
Why should a "required field" notice not be written as label::after { content: " (required)"; } alone?
Show answer
Generated text isn't dependable across assistive technology and can't be searched or translated well. Put the word in the HTML, or use the required attribute so the state is exposed to assistive technology. Use ::after only for an extra visual cue, such as an asterisk.