This document is in beta. Help us by reporting issues via Github or email.
It should be easy to tell which element has keyboard focus.
On this page:
See Rob Dodson explain ‘Focus rings’ in this short video:
Some sites suppress the browser’s default focus indicator (using outline: 0
or none
), without replacing it with a better focus indicator.
By default, native HTML elements have a visual focus indicator provided by the browser. Therefore, links and focusable elements must not have their outline suppressed via CSS, unless a custom focus indicator is provided.
// Never do this, unless you’ve already replaced the default focus outline with
better custom focus styles!
<head>
<style>
a /* or a:focus */ {
outline: none; /* or outline: 0;*/
}
</style>
</head>
<body>
<!-- -->
<a href="...">Next</a>
</body>
// Doing `outline: none` or `0` is fine is you're replacing the browser default
focus outline with better styles.
<style>
a:focus {
box-shadow: 0 0 0 0.2rem ff40b8;
outline: none;
}
</style>
<!-- -->
<a href="...">Next</a>
:focus-visible
polyfillBrowsers’ default focus styles don’t appear when people click or tap buttons or links.
However, if you define your own enhanced styles using the CSS :focus
pseu-do class, those styles will appear on click/tap as well.
Sometimes clients or designers don’t like to see a focus ring appear on a custom button or element when it gets clicked. They might say to a developer: “Get rid of that focus ring”.
But if you got rid of focus rings entirely, the interface would become unusable for keyboard users.
As of Feb 2020 :focus-visible
is only supported by default in Firefox.
But the :focus-visible
polyfill is small and robust.
:focus-visible
and backwards compatibility by The Paciello Group2.4.7 Focus Visible: Any keyboard operable user interface has a mode of operation where the keyboard focus indicator is visible. (Level AA)
See the W3C’s detailed explanation of this guideline with techniques and examples.
This document is in beta. Help us by reporting issues via Github or email.