CSS :target Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
In-Page Navigation

What You’ll Learn

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.

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.

📝 Syntax

The syntax for the :target pseudo-class is:

syntax.css
:target {
  /* CSS properties */
}

You can also scope :target to specific elements:

scoped-target.css
.section:target {
  background-color: #eff6ff;
  border-left: 4px solid #2563eb;
}
:target { } href="#id" id="id" scroll-behavior

How URL Fragments Work

URLMatched element
/docs#intro<div id="intro">
/page#faq<section id="faq">
/page (no hash)No element is :target

Syntax Rules

  • The targeted element must have an id that matches the URL hash exactly.
  • Anchor links use href="#section-id" to activate :target.
  • Only one element is :target at a time — the current hash match.
  • Pair with scroll-margin-top so fixed headers do not cover targeted sections.
  • Add html { scroll-behavior: smooth; } for animated scrolling between sections.

Related Topics

  • HTML a tag — anchor links that set URL fragments
  • :link — styles unvisited anchor links
  • #id selector — targets elements by id in CSS
  • scroll-behavior — smooth scrolling to hash targets
  • ::selection — previous topic in this series

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class (URL-driven)
What it targetsElement whose id matches URL #hash
TriggerClicking href="#id" or loading a URL with a hash
Common pattern:target { background: #eff6ff; border-left: 4px solid #2563eb; }
Smooth scrollhtml { scroll-behavior: smooth; }
Browser supportAll modern browsers

When to Use :target

:target shines in navigation-heavy layouts:

  • Documentation pages — Highlight the section linked from a table of contents.
  • Single-page layouts — Show which content block is currently active.
  • FAQ pages — Expand or highlight the question matching the shared URL hash.
  • CSS-only modals — Reveal a hidden panel when #modal is in the URL.
  • Deep links — Let users bookmark or share links to specific page sections.

👀 Live Preview

Click a link below to jump to a section. The active section receives :target styles:

Section 1

Introduction content. Click “Section 1” above to highlight this block.

Section 2

Features overview. The blue left border appears when this section is targeted.

Section 3

Getting started guide. Only one section is :target at a time.

Watch the URL bar — it updates to #tgt-demo-s2 etc. when you click a link.

Examples Gallery

Practice :target with section navigation, table-of-contents highlighting, smooth scrolling, and a CSS-only modal.

📜 Core Patterns

Highlight sections when anchor links update the URL hash.

Example 1 — Basic :target section highlight

Style the active section with a green background and left border when its id matches the URL hash.

target-sections.html
<nav>
  <a href="#section1">Section 1</a>
  <a href="#section2">Section 2</a>
</nav>

<div id="section1" class="section">...</div>
<div id="section2" class="section">...</div>

<style>
  .section {
    padding: 1rem;
    background: #f8fafc;
    border-left: 4px solid transparent;
  }
  :target {
    background: #dcfce7;
    border-left-color: #16a34a;
  }
</style>
Try It Yourself

How It Works

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.

target-toc.css
.chapter {
  padding: 1.25rem;
  margin-bottom: 1rem;
  border-radius: 0.5rem;
  background: #f8fafc;
  scroll-margin-top: 2rem;
}

.chapter:target {
  background: #eff6ff;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}
Try It Yourself

How It Works

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.

target-smooth.css
html {
  scroll-behavior: smooth;
}

section:target h2 {
  color: #2563eb;
}

section:target {
  outline: 2px solid #93c5fd;
  outline-offset: 4px;
}
Try It Yourself

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.

target-modal.css
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.6);
}

.modal:target {
  display: flex;
  align-items: center;
  justify-content: center;
}
Try It Yourself

How It Works

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.

💬 Usage Tips

  • Use meaningful idsid="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 scrollhtml { 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.

⚠️ 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.

♿ 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.
  • Modal traps — CSS-only modals lack focus trapping; add JavaScript for complex dialog patterns.
  • 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.

🖥 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 Chrome 1+ · Desktop & Mobile
Full support
Mozilla Firefox 1+ · Desktop & Mobile
Full support
Apple Safari 3.1+ · macOS & iOS
Full support
Microsoft Edge 12+ / 79+ Chromium
Full support
Opera 9.5+
Full support
:target pseudo-class 100% supported

Bottom line: Safe to use everywhere. Pair with scroll-behavior: smooth for enhanced UX in modern browsers.

🎉 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.

💡 Best Practices

✅ Do

  • Use descriptive, URL-friendly id values
  • Add scroll-margin-top for fixed navigation
  • Scope with classes: .section:target
  • Enable scroll-behavior: smooth for polish
  • Ensure content works without a hash in the URL

❌ Don’t

  • Forget the id attribute on target elements
  • Duplicate id values on the same page
  • Hide essential content behind :target-only reveals
  • Rely on CSS-only modals for complex accessibility needs
  • Use cryptic hash names users cannot understand

Key Takeaways

Knowledge Unlocked

Five things to remember about :target

Use these points when building in-page navigation.

5
Core concepts
id 02

id required

Must match.

HTML
📍 03

One active

Single target.

Rule
scroll 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with :target, in-page navigation, and CSS-only modals.

HTML Editor →

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.

5 people found this page helpful