Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:focus-within — focus on an element or its descendant

The :focus-within pseudo-class matches an element with focus or a focused descendant, letting you highlight an entire control group.

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

Syntax

:focus-within

:focus-within is a pseudo-class without arguments. It matches an element that itself matches :focus, or has a descendant meeting the focus conditions in the flat tree, which accounts for Shadow DOM.

Unlike :focus, it therefore lets you style a container when a control inside it has focus. It does not move focus to the container.

In the example, .group:focus-within makes the border blue while First or Second has focus. Moving between them preserves the highlight; focusing Outside restores the gray border. The rule does not remove the control’s own focus indicator.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:focus-within example</title>
<style>
  .group {
    padding: 1rem;
    border: 3px solid gray;
  }
  .group:focus-within {
    border-color: blue;
  }
</style>
<div class="group">
  <label>First <input type="text"></label>
  <label>Second <input type="text"></label>
</div>
<label>Outside <input type="text"></label>
</html>

Result

Focus First or Second: the group border is blue. Focus Outside: the group border is gray.

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