
outline: none Locks Out Keyboard Users: Focus Styles Done Right
- كاتب موثّق
- WCAG 2.2
محتويات المقال
*:focus { outline: none; } to the base stylesheet, and the ticket is closed. Two weeks later, a customer who navigates with the keyboard reports that the page "does random things". He pressed Enter on what he believed was "Save", but focus was two controls further, on "Delete address". Nothing on the screen showed him where he was.
The common misconception
outline: none also shows up in plenty of CSS snippets and resets, so it looks like standard practice. And whoever tests the change with a mouse sees nothing break, because for them nothing does.
What actually happens
Focus is the keyboard user's cursor
The browser already hides it from the mouse
:focus-visible, not :focus. MDN describes :focus-visible as matching when the browser's heuristics decide that focus should be made evident, and notes that many browsers show their focus ring in exactly that case. We checked three cases in headless Chromium:
Mouse click on a button: :focusmatched,:focus-visibledidn't, and no ring was drawn.Mouse click into a text field: :focus-visiblematched and the ring showed, since you need to see where typing goes.Tab to the same button: :focus-visiblematched and the ring showed.
outline: none removes it for everyone
*:focus matches every kind of focus, keyboard focus included. With that rule in place, tabbing to the button in Chromium left it with outline-style: none: focus moved, and nothing on the screen changed.
Visible also means not covered
scroll-padding-top: 80px on html, the approach WCAG documents as technique C43, none of them did.
/* "Remove the ugly ring" */*:focus { outline: none;}/* A clear ring wherever the browser shows focus */:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 2px;}/* Keep focused items out from under the sticky header */html { scroll-padding-top: 80px;}:focus-visible, which skips most mouse clicks on buttons | |
outline: none only changes the look | |
How to apply it in your project
Find every removal Search your CSS and any reset you import for outline: noneandoutline: 0on:focusselectors. Delete them, or move the custom styling to:focus-visible.Define one strong indicator Write a single :focus-visiblerule with a thick outline and an offset, in a color with at least 3:1 contrast against every background it sits on. Override it per component only when a background requires it.Make room for sticky UI Set scroll-padding-topandscroll-padding-bottomonhtmlto the height of your sticky header and footer, and make sure cookie banners can't cover the element that has focus.Tab through every page Put the mouse away and walk each page with Tab and Shift+Tab. Every stop needs a visible, uncovered ring. Repeat once in forced colors mode, using a Windows contrast theme or the forced-colors emulation in your browser's DevTools.
Key takeaways
The focus indicator is the keyboard user's cursor; removing it fails WCAG 2.4.7 Focus Visible (Level AA). :focusmatches every focus, while:focus-visiblematches only when the browser decides focus should be shown.Restyle :focus-visiblewith a thick, high-contrast outline instead of deleting the ring.A visible ring must also stay uncovered: WCAG 2.2's 2.4.11 targets sticky headers and banners, and scroll-paddingfixes most cases.Prefer outlineoverbox-shadow: it doesn't shift the layout and it survives forced colors mode.
التعليقات (0)