Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :focus pseudo-class

Reference for the CSS :focus pseudo-class: style the element that has input focus, with syntax, examples, accessibility advice, usage notes and browser support.

The :focus pseudo-class matches the element that currently has focus: the one that will receive keystrokes. Form fields, buttons, links and any element with a tabindex can be focused, by clicking, tapping, or moving with the Tab key.

Focus styling is an accessibility feature. People who navigate with a keyboard rely on it to know where they are on the page.

CSS
input:focus {
  border-color: #2563eb;
}

Try it

Highlight the focused field

Syntax

CSS
:focus { /* declarations */ }

:focus has no arguments and a specificity of (0,1,0). Only one element in a document (per focus context) matches at a time, plus, in practice, only while the window is active.

Syntax and arguments

Part Description
:focus Matches the element that currently has focus.
No arguments The pseudo-class is not functional.
Combining a:focus, .card button:focus; to style an ancestor of the focused element use :focus-within.

Usage notes

  • Never remove the outline without a replacement. outline: none leaves keyboard users lost. If you replace the default ring, make the new indicator clearly visible against its background.
  • Mouse clicks also match. Clicking a button focuses it in most browsers, so a strong :focus style appears on every click. Many designs use :focus-visible for the ring and keep :focus minimal.
  • Not every element is focusable. A plain <div> never matches unless it has tabindex. Use tabindex="0" only for custom controls that really need keyboard access.
  • Safari and buttons. Some browsers do not focus buttons on click, so do not rely on :focus appearing after a mouse press.
  • Contrast. The indicator should stand out from both the element and its surroundings; a thin pale line is easy to miss.

Browser support

Supported in every browser.