CSS Button Styles You Might Not Know | David Bushell
Visual Focus
Accessibility is for everyone. I like using keyboard navigation to tab through interactive elements. Sadly I’ve lost count of how many clients have asked me to “remove that ugly border” around focused buttons.
Button focus state can be improved with two tricks:
.button {
&:focus-visible {
outline: 2px solid magenta;
outline-offset: 2px;
}
}
First, I replace the default focus pseudo-class with focus-visible. MDN has a long section on focus vs focus-visible. Basically it’s what the original should have been in hindsight.
https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible#focus_vs_focus-visible
Second, I use outline-offset to give some breathing room between the focus ring and the button itself. If the button has a prominent border style the outline would sit flush against it looking like a double border. Adding an offset makes the focus state more visible.
One thing to note is that ::file-selector-button does not get focus, rather the parent input does, so apply styles there.