Sub topic 15
Accessibility: focus indicators and generated content
This chapter assumes you know basic HTML, CSS rule syntax, and the pseudo-class and pseudo-element syntax from earlier chapters. It covers three accessibility problems that pseudo selectors cause often: missing focus indicators, generated content that assistive technology reads or ignores, and state cues that rely on color alone.
Why pseudo selectors need extra care
Pseudo-classes such as :hover and :focus describe how people interact with a page. Pseudo-elements such as ::before and ::after add content that is not in the HTML. Both can help users, and both can lock some users out. Keyboard users need to see where they are on the page. Screen reader users may hear generated text that sighted users never think about. Some users cannot tell two states apart by color.
Focus indicators
A focus indicator is the visible mark that shows which element will receive keyboard input. Tab through any page and you rely on it. If you remove it, keyboard users cannot tell where they are.
Never remove the outline without a replacement
The most common mistake is this rule:
/* Avoid: removes the only cue keyboard users have */
a:focus,
button:focus {
outline: none;
}Browsers draw a default outline on focused elements. Setting outline: none removes it. If you do this, you must provide another indicator that is at least as visible.
Use :focus-visible
:focus matches whenever an element has focus, including after a mouse click. That is why many designers disliked the default ring on buttons. :focus-visible matches only when the browser decides a visible indicator helps. In practice that means keyboard navigation, and text inputs, which always show it.
button:focus-visible,
a:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}This keeps the ring for keyboard users and hides it after a mouse click on a button. You do not have to remove anything to get that result. The browser's default styling already uses the same logic in current browsers.
Replace, don't delete
If you want a custom style, set one in place of the default. Prefer outline over box-shadow or border for the ring. In Windows High Contrast mode (forced colors), the browser keeps outlines but can drop shadows and some backgrounds. A shadow-only indicator may vanish for those users.
.btn:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
box-shadow: 0 0 0 5px rgba(26, 115, 232, 0.25);
}Here the outline guarantees visibility. The shadow only adds polish.
Contrast and size
The WCAG 2.2 guidelines ask that the focus indicator contrast with the surrounding colors. Aim for a contrast ratio of at least 3:1 against adjacent colors, and a thickness of at least 2 CSS pixels. A 3px outline with an outline-offset of 2px usually meets both. Check the ring against the background it appears on, because a blue ring that works on white can disappear on a dark blue header.
:focus-within for containers
:focus-within matches a container when it or any descendant has focus. It is useful for showing which form group the user is in.
.form-group:focus-within {
background: #f0f6ff;
}Treat this as an addition to the control's own focus ring, not a replacement. The input still needs its own visible indicator.
Do not rely on hover alone
:hover has no equivalent for keyboard or touch users. If a menu opens on hover, pair it with focus.
.menu-item:hover > .submenu,
.menu-item:focus-within > .submenu {
display: block;
}Every rule that reveals something on :hover should have a matching :focus-visible or :focus-within rule.
Generated content and assistive technology
The content property on ::before and ::after creates text or shapes that are not in the document. Browsers expose this content to the accessibility tree, which is the structure that screen readers read from. Support varies, and the behavior has changed over time. Treat generated text as something that may or may not be spoken.
Decorative content only
Use generated content for decoration: arrows, bars, icons, and quotation marks. Do not put meaningful information there.
.external::after {
content: " \2197";
}A screen reader may announce this arrow as "north east arrow" after every external link. That is noise. It also conveys the fact that the link leaves the site only visually.
Alternative text for generated content
The content property accepts an alternative text after a slash. The text after the slash is what assistive technology uses. An empty string tells it to skip the content.
.external::after {
content: " \2197" / "";
}Browsers that do not understand the slash syntax drop the whole declaration. To stay safe, write a plain fallback first and the slash version second.
.external::after {
content: " \2197";
content: " \2197" / "";
}If the information matters, put it in the HTML instead. For example, add visually hidden text such as "(opens in a new tab)" inside the link. Generated content is the wrong place for it.
Text in generated content cannot be selected or found
Users cannot select generated text, and the browser's find-in-page does not match it. Translation tools may also skip it. Do not generate labels, prices, or instructions with content.
/* Avoid: the unit is invisible to copy, search, and some tools */
.price::after {
content: " USD";
}Write the text in the HTML:
<span class="price">19.99 USD</span>Empty content and purely visual shapes
A ::before with content: "" that draws a bar or shape is fine. It carries no text, so there is nothing to announce.
.card::before {
content: "";
display: block;
height: 4px;
background: #1a73e8;
}Icon fonts and symbols
Icon fonts put private-use characters in content. A screen reader may read them as nothing, or as a strange character. Provide the accessible name another way, such as an aria-label on the button, and use the slash syntax to hide the icon.
.icon-close::before {
content: "\e5cd" / "";
font-family: "Icons";
}State cues beyond color
Pseudo-classes often express state with color alone, such as a red border for :invalid. About one in twelve men has some form of color vision deficiency, and a red border may look the same as a gray one to them. WCAG calls this the "use of color" criterion.
Add a second cue: an icon, text, a thicker border, or a different shape.
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
border-width: 2px;
background: url("error.svg") no-repeat right 0.5rem center / 1.25rem;
}The error message text near the field is the strongest cue. A pseudo-class can style the field, but the message should live in the HTML and be linked with aria-describedby.
The same applies to checked states. A custom checkbox that changes only color on :checked needs a visible check mark.
input:checked + label {
font-weight: 600;
}
input:checked + label::before {
content: "\2713 " / "";
}Other accessibility checks
:disabledcontrols are skipped by the keyboard and are usually exempt from contrast rules, but keep the text legible.opacity: 0.5on pale text can make it unreadable.::placeholderis not a label. Its default color is often low contrast, so use at least 4.5:1, such as#767676on white. Always give inputs a real<label>.::selectionchanges the highlight. Pick colors with enough contrast between text and highlight, or leave the default.:visitedstyling is restricted by browsers for privacy, so you can only change a few color properties. Keep visited links distinguishable from unvisited ones by more than a slight hue shift.- Do not use
display: noneorvisibility: hiddento hide an element and expect:focusto work on it. Hidden elements cannot receive focus.
A quick audit
- Press Tab through the whole page. Is the focused element always visible?
- Check that every
:hoverreveal also works with the keyboard. - Search your CSS for
outline: noneandoutline: 0. Each should have a replacement. - Search for
content:with real words in it. Move meaningful text to HTML. - Turn on forced colors or grayscale in your browser's developer tools. Can you still see every state?
Exercise
A designer asks you to remove the blue ring that appears when a button is clicked with the mouse. Which approach keeps the page usable for keyboard users, and why?
Show answer
Do not use button:focus { outline: none; }. Instead, style button:focus-visible with your own outline (for example outline: 3px solid #1a73e8; outline-offset: 2px;). Browsers apply :focus-visible mostly for keyboard focus, so the ring stays for keyboard users and usually does not appear after a mouse click.
Exercise
A link has a::after { content: " (opens in a new tab)"; }. Name two problems with this approach and one fix.
Show answer
The text cannot be selected or found with find-in-page, and screen readers may or may not announce it, so the information is unreliable. It is also not translated by some tools. The fix is to put the text in the HTML, for example in a visually hidden <span> inside the link, and keep any pseudo-element purely decorative.
Exercise
An input shows a red border for :invalid. Why is this not enough, and what would you add?
Show answer
Users with color vision deficiency may not see the difference between a red and a normal border. Add a second cue such as an error icon or a thicker border, and put a text error message in the HTML linked to the input with aria-describedby.