Home CSS Selectors :target CSS :target Pseudo-class Definition
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.
Syntax
CSS Syntax :target { /* declarations */ }
Often typed as p:target, section:target, or #panel:target.
Approach
How to Use :target Give the destination an id, link to it with #, then style :target.
Add a unique id on the destination element. Link with href="#that-id". Write :target (or p:target) styles. Click the link — the matching element becomes the target. Piece Required? Role Element id Yes Must match the URL fragment Hash link (#id) Yes* Sets the fragment (*or load URL with a hash) :targetYes Styles the matching element No hash — Document has no target element
Try it
Live Preview Simulate a URL fragment change. In a real page, clicking #one / #two would make that block match :target.
Code
CSS Examples Each snippet focuses on :target. Use Try It Yourself for a live editor (click the hash links there).
1. Highlight a paragraph 2. Highlight a section 3. Show a panel Example 1 — Highlight a Linked Paragraph Style the paragraph that matches the URL hash.
<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> Jump to paragraph
This paragraph becomes the target.
Not targeted.
How It Works Id. id="p1" names the destination.Link. href="#p1" sets the fragment.Rule. p:target styles that paragraph when active.Other p. Different id — no match.Example 2 — Highlight a Section Outline the section that the hash points to.
<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> Go to setup
Setup Install steps live here.
How It Works Section id. id="setup" matches #setup.Selector. section:target limits the style to sections.Outline. Makes the jumped-to block obvious.TOC pattern. Same idea for long articles.Example 3 — Reveal a Panel with :target Hide a panel until its hash is active.
<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="javascript:void(0)">Close</a>
</div> Open info
Panel content Close
How It Works Hidden by default. #info { display: none; }Open. href="#info" makes the div match :target.Show rule. #info:target { display: block; }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 :targetTarget 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.
Compatibility
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.
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.
Remember
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 What does the CSS :target pseudo-class do? 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.
Does the element need an id? Yes. :target only matches when an element's id exactly matches the current hash. No matching id means nothing is targeted.
Can more than one element be :target? No. Only one fragment is active at a time, so only one element matches. Changing the hash moves :target to the new id.
Does history.pushState() update :target? No. The target is set on load and on history.back/forward/go. pushState and replaceState do not change :target.
Is :target widely supported? Yes. It is Baseline Widely available. It is a browser CSS selector, not a Node.js or runtime API.
Can I build a CSS-only panel with :target? Yes. Hide a panel by default and show it with #panel:target { display: block; }. A close link can point to # or another id.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion