Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:target-before — markers before the active scroll marker

The :target-before pseudo-class selects every marker preceding the active marker within the same scroll marker group.

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

Syntax

a:target-before { /* declarations */ }

:target-before matches scroll markers preceding the active marker within the same group, according to flat tree order rather than screen position. It takes no arguments and does not select only the immediate predecessor.

:target-current identifies the active marker; :target-after matches following markers. In the example, scroll-target-group: auto groups the links; the links are styled, not their target sections.

CSS Overflow Module Level 5 defines this feature in an editor’s draft. Verify behavior in the target browser; Chromium 152 scope metadata does not prove support or example correctness.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:target-before demo</title>
<style>
  html { scroll-behavior: auto; }
  body { margin: 0; font: 18px sans-serif; }
  nav {
    position: fixed;
    top: 0;
    right: 0;
    background: white;
    padding: 12px;
    scroll-target-group: auto;
  }
  nav a { color: black; }
  nav a:target-before { color: green; }
  nav a:target-current { font-weight: bold; }
  section { height: 100vh; }
</style>
<nav aria-label="Sections">
  <a href="#one">One</a>
  <a href="#two">Two</a>
  <a href="#three">Three</a>
</nav>
<section id="one">One</section>
<section id="two">Two</section>
<section id="three">Three</section>
</html>

Result

With the required features supported, after activating Two and allowing scrolling to settle: One is green, Two is bold, and Three remains black.

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