<p>Always worth repeating: all interactive elements should have a focus style. That way, a keyboard user can tell when they have moved focus to that element. </p> <p>But if you use :focus alone for this, it has a side effect that a lot of people don’t like. It means that when you click (with a mouse) on an interactive element, you’ll see the focus style. Arguably, you don’t need that feedback as a mouse user, because you just moved your cursor there and clicked. Whatever you think of that, it’s annoyed so many people over the years that they remove focus styles entirely, which is a gnarly net loss for accessibility on the web. </p> <p>What if we could apply focus styles only when the keyboard is used to focus something, not the mouse? Lea Verou put a finger on this a few years back: </p> <p>I’m gonna start blanket adding the following rule to all my stylesheets::focus:not(:focus-visible) { outline: none }Gets rid of the annoying outline for mouse users but preserves it for keyboard users, and is ignored by browsers that don’t support :focus-visible.— Lea Verou (@LeaVerou) September 28, 2018 </p> <p>That was in response to Chrome dropping the feature behind a flag. Clever clever. </p> <p>Fast forward a couple of years, Chrome is releasing it without a flag. They are on board with Lea’s idea: </p> <p>By combining :focus-visible with :focus you can take things a step further and provide different focus styles depending on the user’s input device. This can be helpful if you want the focus indicator to depend on the precision of the input device: </p> <p>/* Focusing the button with a keyboard will show a dashed black line. */<br /> button:focus-visible {<br /> outline: 4px dashed black;<br /> } </p> <p>/* Focusing the button with a mouse, touch, or stylus will show a subtle drop shadow. */<br /> button:focus:not(:focus-visible) {<br /> outline: none;<br /> box-shadow: 1px 1px 5px rgba(1, 1, 0, .7);<br /> } </p> <p>I might suggest trying those selectors without the button, making them globally applied! </p> <p>There is more to dig into, so I’ll link up some more stuff here: </p> <p>The Chromium Blog post covers the heuristics of the selector. It’s tricky. It’s like there is an algorithm to determine if :focus-visible is going to match or not, which you just largely need to trust. It also covers the idea that Firefox has long had :-moz-focusring, but the behavior is different enough that they don’t recommend using it if you’re shooting for consistent behavior. Matthias Ott blogged about it with some good info, like using the official polyfill and how to look at the styles properly in DevTools (there is a new checkbox for it). We’ve covered this before. In that, we noted Lea’s tweet that she thought usage would explode when it ships for real. Let’s see (and hope)!Our almanac entry has a bunch of details. </p> <p>The post The :focus-visible Trick appeared first on CSS-Tricks.<br /> You can support CSS-Tricks by being an MVP Supporter.</p>

Breakdown

Always worth repeating: all interactive elements should have a focus style. That way, a keyboard user can tell when they have moved focus to that element.

But if you use :focus alone for this, it has a side effect

...