In this video, you made focus visible on the choir’s site, and kept it out from under the sticky header.
Key Points
- The starting stylesheet’s base layer removed the outline from every link and button with nothing in its place, so pressing Tab moved focus without anything on the page showing it. That fails 2.4.7 Focus Visible (Level AA).
- Watch out for hover styles. Pressing Tab never triggers
:hover, so a hover style isn’t a focus style. - Deleting the rule would bring back the browser’s thin default ring. A bare
:focus-visiblerule replaces it and covers every control, in the site’s own focus colors. :focus-visiblematches the focused element when the browser decides you need to see it. In Chrome, clicking Ticket prices with the mouse moved focus to the dialog’s Getting to the venues link, which matched:focusbut not:focus-visible, so no ring appeared. Pressing Tab moved focus to Close, and the ring appeared.:focusmatches whenever an element has focus, so a rule on it would ring the link after a click. Clicks can count for:focus-visibletoo. A text field matches it after a click.:focus-withinmatches a container, such as the dialog, while anything inside it has focus. It’s for styling the container, so the ring skips it.- The components layer’s
.site-headerrule sets--color-focusto--color-focus-on-brand, so the one ring rule draws yellow in the dark header, at 9:1, and red on the page, at 6.25:1. Both clear the 3:1 that 1.4.11 Non-text Contrast asks of a focus ring. - An outline stays in forced colors, where a box shadow is dropped.
- The browser doesn’t scroll to a focused control that’s already in the window. In a short window, walking back up the Concerts page with Shift+Tab put focus on Ticket prices while it sat under the sticky header, so the header covered it, ring and all. That fails 2.4.11 Focus Not Obscured (Minimum) (Level AA).
scroll-padding-toptells the browser that the top of the window is taken. When it scrolls, it treats that strip as outside the window.
The Final Code
In the base layer, the rule that removed the outline is now a focus ring:
:focus-visible {
outline: 3px solid var(--color-focus);
outline-offset: 2px;
}
Every control now shows a ring when focus reaches it from the keyboard. It’s yellow in the header and red on the page.
The header is 67 pixels tall, a little over 4rem, so the top of the base layer pads the top of the window by 5rem, leaving room for the ring:
html {
scroll-padding-top: 5rem;
}
Walking back up the Concerts page with Shift+Tab now scrolls Ticket prices out from under the header. At 200% zoom, the walk ends the same way. Zoom scales rem and pixels alike, so the header still measures 67 pixels, Ticket prices still comes out with its ring, and the 5rem needs no change.
Next comes Add a Skip Link and Hide Closed Menus, where you’ll give keyboard users a way past the header and keep a closed menu’s links out of their way.