Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:focus — the focused element

The :focus pseudo-class matches an element that has focus: the recipient of input, such as keyboard input. It lets you highlight a focused form field, but does not itself move focus or make an element capable of receiving it.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

:focus

:focus is a state pseudo-class with no arguments. The selector input:focus matches an input while it has focus. Which elements can receive focus depends on the document language and browser. The selector itself does not make an element focusable, but CSS declarations can prevent it from receiving focus. For example, setting display: none on an otherwise focusable input prevents it from receiving focus.

Focus is not the same as pointing at an element (:hover) or temporarily activating it (:active). :focus matches a focused element, whereas :focus-visible additionally reflects the browser's decision about whether to show a focus indicator. Do not remove a visible indicator without providing a clear replacement.

:focus does not automatically propagate to a parent merely because its descendant has focus; use :focus-within for that matching behavior. HTML does define additional rules, including for a Shadow DOM host, so do not assume that exactly one element always matches.

In the example, click First, then press Tab. The expected result is that the blue outline moves to Second. The example requires local verification; accessibility settings may change the displayed colors.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:focus example</title>
<style>
  input:focus {
    outline: 3px solid #005fcc;
    outline-offset: 3px;
  }
</style>
<label>First <input type="text"></label>
<label>Second <input type="text"></label>
</html>

Result

The focused input has a 3px solid blue outline, offset by 3px. Moving focus to the other input moves this outline to that input.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources