Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:target — the element addressed by a URL fragment

The :target pseudo-class selects the document's current target element. In typical HTML, a link with the fragment #alpha points to the element with id="alpha", allowing it to be highlighted without JavaScript.

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

Syntax

:target { /* declarations */ }
section:target { /* declarations */ }

:target is a pseudo-class without arguments. In HTML, it matches the document's current target element; if there is no target element, it matches nothing. Typically, a URL fragment identifies the target: #alpha refers to the element with id="alpha".

The selector section:target restricts the match to a section element. The styling applies to the navigation destination, not the link pointing to it. Do not confuse :target with a link's target attribute: that attribute specifies where to open the document.

In the example, open the file without a URL fragment. Both sections should be white. Clicking Alpha should give only section alpha a gold background; clicking Beta should move the highlight to section beta. The CSS rule only defines the appearance—it does not itself perform navigation.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:target demo</title>
<style>
  section { padding: 12px; background-color: white; }
  section:target { background-color: gold; }
</style>
<nav>
  <a href="#alpha">Alpha</a>
  <a href="#beta">Beta</a>
</nav>
<section id="alpha">Alpha</section>
<section id="beta">Beta</section>
</html>

Result

Without a URL fragment: both sections have white backgrounds.
After clicking Alpha: the URL fragment is #alpha; only Alpha has a gold background.
After clicking Beta: the URL fragment is #beta; only Beta has a gold background.

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