The :target pseudo-class styles the element that matches the URL hash — the #section-id fragment. It is perfect for highlighting active sections, table-of-contents navigation, and even CSS-only modals.
01
URL hash
#fragment.
02
id match
id="..."
03
Highlight
Active section.
04
Anchor links
href="#id"
05
Modals
CSS-only UI.
06
Smooth scroll
scroll-behavior.
Fundamentals
Introduction
The CSS :target pseudo-class matches an element when its id attribute equals the URL fragment identifier — the part after # in the address bar. For example, when the URL is page.html#pricing, the element <section id="pricing"> becomes :target.
This is a powerful, JavaScript-free way to highlight the section a user navigated to via an anchor link. It is commonly used on documentation pages, single-page layouts, and long articles with a table of contents.
Definition and Usage
Apply :target styles to give visual feedback when a section is active. Typical patterns include changing background-color, adding a colored border-left, or applying a subtle box-shadow. Only one element can be :target at a time — whichever matches the current hash.
💡
Beginner Tip
Click the navigation links in the Live Preview below. Each jump updates the URL hash, and the matching section receives :target styles automatically — no JavaScript required.
Clicking href="#section1" scrolls to the div and sets the URL hash. The matching element becomes :target and receives the green highlight until another hash is selected.
Example 2 — Table of contents navigation
Build a sidebar TOC where the linked chapter gets a blue glow when targeted.
Scoping to .chapter:target limits the effect to chapter blocks. scroll-margin-top prevents fixed navigation bars from covering the top of the targeted section.
📄 Advanced Patterns
Combine :target with smooth scrolling and modal overlays.
Example 3 — Smooth scroll with :target
Add animated scrolling so jumping between sections feels polished.
Page scrolls smoothly to the target section; its heading turns blue with a light outline.
How It Works
scroll-behavior: smooth animates the scroll when a hash link is clicked. section:target then styles the arrived-at section, giving users clear visual feedback after the scroll completes.
Example 4 — CSS-only modal with :target
Reveal a hidden overlay when the URL hash matches the modal’s id — no JavaScript needed.
A link with href="#info-modal" sets the hash. The hidden .modal becomes :target and switches to display: flex. A close link pointing to # clears the hash and hides the modal again.
Tips
💬 Usage Tips
Use meaningful ids — id="pricing" is clearer in URLs than id="s3".
Add scroll-margin-top — Prevents fixed headers from hiding the top of targeted sections.
Combine with smooth scroll — html { scroll-behavior: smooth; } improves in-page navigation UX.
Scope with classes — .section:target avoids styling unintended elements.
Shareable deep links — Users can bookmark page.html#faq and land directly on that section.
Subtle highlights — A light background shift or left border is often enough visual feedback.
Watch Out
⚠️ Common Pitfalls
Missing id — Without a matching id, :target never applies.
Duplicate ids — Invalid HTML; only the first match may receive :target.
Case sensitivity — #Section does not match id="section" in HTML.
Fixed header overlap — Targeted content can hide under sticky nav without scroll-margin-top.
Modal accessibility — CSS-only modals need focus management; consider ARIA for production apps.
Over-reliance on hash — Content must remain usable when users arrive without a hash.
A11y
♿ Accessibility
Keyboard navigation — Anchor links are keyboard-focusable; ensure visible :focus styles on nav links.
Do not hide content — :target should highlight, not be the only way to reveal essential information.
Skip links — Pair in-page nav with a skip-to-content link for screen reader users.
Color contrast — Target highlight colors should keep text readable.
🧠 How :target Works
1
User clicks anchor link
A link like href="#pricing" is activated.
Click
2
URL hash updates
The browser sets the fragment to #pricing and scrolls to the element.
URL
3
:target matches
The element with id="pricing" becomes :target.
CSS
=
📍
Active section styled
Your highlight styles apply until the user navigates to a different hash.
Compatibility
🖥 Browser Compatibility
The :target pseudo-class is supported in all modern browsers and has been for many years.
✓ Baseline · Universal support
URL fragment styling everywhere
:target works in Chrome, Firefox, Safari, Edge, and Opera. It is one of the oldest and most reliable CSS pseudo-classes.
100%Global support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox1+ · Desktop & Mobile
Full support
Apple Safari3.1+ · macOS & iOS
Full support
Microsoft Edge12+ / 79+ Chromium
Full support
Opera9.5+
Full support
:target pseudo-class100% supported
Bottom line: Safe to use everywhere. Pair with scroll-behavior: smooth for enhanced UX in modern browsers.
Wrap Up
🎉 Conclusion
The :target pseudo-class is a handy tool for interactive, hash-driven styling without JavaScript. It highlights active sections, powers table-of-contents navigation, and even enables CSS-only modals.
Remember that the element needs a matching id, only one element is :target at a time, and scroll-margin-top helps when fixed headers are present.
Rely on CSS-only modals for complex accessibility needs
Use cryptic hash names users cannot understand
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :target
Use these points when building in-page navigation.
5
Core concepts
#01
URL hash
Fragment match.
Trigger
id02
id required
Must match.
HTML
📍03
One active
Single target.
Rule
scroll04
Smooth scroll
UX polish.
Tip
🌐05
100% support
All browsers.
Compat
❓ Frequently Asked Questions
The :target pseudo-class matches the element whose id matches the URL fragment — the part after the # symbol. When a user clicks a link like href="#section2", the element with id="section2" becomes :target and receives your styles.
Yes. :target only matches elements with an id that exactly matches the URL hash. Without a matching id, no element is targeted and :target styles do not apply.
Only one element can be :target at a time — the one matching the current URL fragment. When the hash changes, the previous target loses :target and the new matching element gains it.
Yes. A common pattern hides a panel by default and uses #modal:target { display: block; } to reveal it when the URL hash matches the modal's id. A close link can point to # to dismiss it.
Yes. :target has been supported in all major browsers for many years, including Chrome, Firefox, Safari, Edge, and Opera.