Entry 01
One focus ring with :focus-visible
Use it as the focus style for every interactive element. Never write outline: none.
Code
:focus-visible {
outline: max(2px, 0.08em) solid currentColor;
outline-offset: 0.25em;
}Why it works, the rules and the credits
Why it works
- The browser decides when a ring helps. Keyboard focus gets one, a mouse click on a button does not, and text inputs always do.
currentColorandemmake the ring follow each component's color and size.outlinetakes no layout space and survives forced-colors mode, which removesbox-shadow.
Rules
- On a filled button
currentColorcan fail contrast. Set the outline to the button's background color there. - A component that draws its ring with
box-shadowkeepsoutline-color: transparent, neveroutline: none, so forced-colors mode can repaint it.
Support
- Chrome
- 86
- Firefox
- 85
- Safari
- 15.4
Credits
Borrowed from: Stephanie Eckles, Modern CSS Upgrades To Improve Accessibility, 2021-04-09, and Smol Focus Styles · Kevin Powell, 3 super small changes to improve your CSS, 2023-03-07