CSS :target Pseudo-class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :target pseudo-class matches the document’s target element — the one whose id equals the URL fragment after #.

Example: https://example.com/help#setup makes <section id="setup"> match :target. Use it to highlight in-page jumps, TOC sections, or simple CSS-only panels.

Baseline · Widely available.

Not deprecated. CSS3. Extremely broad browser support.

Shadow DOM note.

:target may not reach elements inside a shadow tree. Prefer light-DOM ids for fragment styling.

CSS Syntax

syntax.css
:target { /* declarations */ }

Often typed as p:target, section:target, or #panel:target.

Selector Forms

PatternMatchesTry it
p:targetTargeted paragraph from a hash linkTry it
section:targetTargeted page sectionTry it
#panel:targetShow a panel when its hash is activeTry it
:target::beforeAdd a marker inside the targetTry it

How to Use :target

Give the destination an id, link to it with #, then style :target.

  1. Add a unique id on the destination element.
  2. Link with href="#that-id".
  3. Write :target (or p:target) styles.
  4. Click the link — the matching element becomes the target.
PieceRequired?Role
Element idYesMust match the URL fragment
Hash link (#id)Yes*Sets the fragment (*or load URL with a hash)
:targetYesStyles the matching element
No hash—Document has no target element

Live Preview

Simulate a URL fragment change. In a real page, clicking #one / #two would make that block match :target.

Gold background = simulated :target match for the active fragment.

Sections

Jump to one Jump to two

Section one

Section two

Fragment #one — first section highlighted

CSS Examples

Each snippet focuses on :target. Use Try It Yourself for a live editor (click the hash links there).

Example 1 — Highlight a Linked Paragraph

Style the paragraph that matches the URL hash.

CSS
<style>
  p:target {
    background: #fef08a;
  }
</style>

<a href="#p1">Jump to paragraph</a>
<p id="p1">This paragraph becomes the target.</p>
<p>Not targeted.</p>
Try It Yourself

How It Works

  1. Id. id="p1" names the destination.
  2. Link. href="#p1" sets the fragment.
  3. Rule. p:target styles that paragraph when active.
  4. Other p. Different id — no match.

Example 2 — Highlight a Section

Outline the section that the hash points to.

CSS
<style>
  section:target {
    outline: 2px solid #2563eb;
    background: #eff6ff;
  }
</style>

<a href="#setup">Go to setup</a>
<section id="setup">
  <h2>Setup</h2>
  <p>Install steps live here.</p>
</section>
Try It Yourself

How It Works

  1. Section id. id="setup" matches #setup.
  2. Selector. section:target limits the style to sections.
  3. Outline. Makes the jumped-to block obvious.
  4. TOC pattern. Same idea for long articles.

Example 3 — Reveal a Panel with :target

Hide a panel until its hash is active.

CSS
<style>
  #info {
    display: none;
    padding: 0.75rem;
    border: 1px solid #cbd5e1;
  }

  #info:target {
    display: block;
  }
</style>

<a href="#info">Open info</a>
<div id="info">
  Panel content
  <a href="#">Close</a>
</div>
Try It Yourself

How It Works

  1. Hidden by default. #info { display: none; }
  2. Open. href="#info" makes the div match :target.
  3. Show rule. #info:target { display: block; }
  4. Close. Link to # clears the fragment so it hides again.

Edge Cases & Pitfalls

If nothing highlights, check these first.

id

Id must match the hash exactly

#Setup and id="setup" do not match. Spelling and case must agree.

one

Only one target at a time

Changing the fragment moves :target to the new id. The previous element loses the styles.

history

pushState does not update :target

Target updates on load and back/forward navigation, not on history.pushState() / replaceState().

a11y

Do not rely on color alone

Pair highlights with outline, text, or a marker so the jump target is clear for everyone.

shadow

Web components caveat

:target may not apply inside shadow roots. Keep fragment targets in the light DOM when possible.

Browser Support

The :target pseudo-class is a browser CSS selector. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:target

Supported in all modern browsers (CSS3). Safe for teaching and production.

100% Core support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1.3+
Full support
Microsoft Edge 12+
Full support
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full support
:target Universal

Bottom line: No prefixes. No polyfills. It is not a runtime API.

Key Takeaways

  • Role: style the element whose id matches the URL #fragment.
  • Setup: unique id + hash link + :target rule.
  • One at a time: only the current fragment’s element matches.
  • Tip: great for TOC highlights and simple CSS-only panels.

One line: :target styles whatever the URL hash currently points to.

Frequently Asked Questions

It matches the element whose id equals the URL fragment — the part after #. A link to #setup makes the element with id="setup" match :target.
Yes. :target only matches when an element's id exactly matches the current hash. No matching id means nothing is targeted.
No. Only one fragment is active at a time, so only one element matches. Changing the hash moves :target to the new id.
No. The target is set on load and on history.back/forward/go. pushState and replaceState do not change :target.
Yes. It is Baseline Widely available. It is a browser CSS selector, not a Node.js or runtime API.
Yes. Hide a panel by default and show it with #panel:target { display: block; }. A close link can point to # or another id.

Next: type selector

Learn how to style elements by their HTML tag name.

type tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful