Web development

Keep a visible keyboard focus indicator

Use focus-visible to provide a clear focus indicator for keyboard interaction. Preserve a usable native focus state rather than removing outlines without a replacement.

Worked example

HTML example · save as example.html and open in a browser

<style>
a:focus-visible,button:focus-visible{outline:3px solid #a34500;outline-offset:4px}
button{padding:.7rem 1rem}
</style>
<a href="/articles/">Read articles</a> <button type="button">Example action</button>

How it works

The browser decides when focus-visible applies based on the interaction context. The offset separates the ring from the control’s border. A visible ring helps a keyboard user track location without relying on hover or a mouse pointer.

Limits and mistakes

Check contrast against the actual adjacent colors, including dark themes. Focus rings can be clipped by overflow-hidden ancestors. The selector does not make an unfocusable custom element into a keyboard-operable control.

Check the result

Use only Tab and Shift+Tab to move through the example. Confirm every interactive control has a visible indicator and that it is not hidden beneath another element.

Sources

Reference documentation for the behavior used in this guide: