--- title: ':focus' slug: 'Web/CSS/:focus' tags: - ':focus' - CSS - Layout - Pseudo-class - Reference - Selector - Web translation_of: 'Web/CSS/:focus' ---
{{CSSRef}}

The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard's Tab key.

/* Selects any <input> when focused */
input:focus {
  color: red;
}

Note: This pseudo-class applies only to the focused element itself. Use {{CSSxRef(":focus-within")}} if you want to select an element that contains a focused element.

Syntax

{{CSSSyntax}}

Examples

HTML

<input class="red-input" value="I'll be red when focused."><br>
<input class="blue-input" value="I'll be blue when focused.">

CSS

.red-input:focus {
  background: yellow;
  color: red;
}

.blue-input:focus {
  background: yellow;
  color: blue;
}

Result

{{EmbedLiveSample("Examples")}}

Accessibility concerns

Make sure the visual focus indicator can be seen by people with low vision.  This will also benefit anyone use a screen in a brightly lit space (like outside in the sun). WCAG 2.1 SC 1.4.11 Non-Text Contrast requires that the visual focus indicator be at least 3 to 1.

:focus { outline: none; }

Never just remove the focus outline (visible focus indicator) without replacing it with a focus outline that will pass WCAG 2.1 SC 1.4.11 Non-Text Contrast.

Specifications

Specification Status Comment
{{SpecName("HTML WHATWG", "scripting.html#selector-focus", ":focus")}} {{Spec2("HTML WHATWG")}} Defines HTML-specific semantics.
{{SpecName("CSS4 Selectors", "#focus-pseudo", ":focus")}} {{Spec2("CSS4 Selectors")}} No change.
{{SpecName("CSS3 Selectors", "#the-user-action-pseudo-classes-hover-act", ":focus")}} {{Spec2("CSS3 Selectors")}} No change.
{{SpecName("CSS2.1", "selector.html#dynamic-pseudo-classes", ":focus")}} {{Spec2("CSS2.1")}} Initial definition.

Browser compatibility

{{Compat("css.selectors.focus")}}

See also