The :target selector matches the element whose id equals the URL hash fragment — e.g. #foo highlights the element with id="foo". Available since jQuery 1.9; standard CSS pseudo-class.
01
Syntax
:target
02
#hash
Fragment
03
Deep link
Jump to id
04
vs #id
Hash-aware
05
CSS
Standard
06
hashchange
Live update
Fundamentals
Introduction
Long documentation pages, FAQs, and wikis use in-page anchor links like <a href="#pricing"> so readers jump straight to a section. The browser updates the URL hash, and the element with matching id becomes the current target.
jQuery’s :target pseudo-class (since 1.9) selects that hash-referenced element. Official documentation example: for URI https://example.com/#foo, the expression $("p:target") selects the <p id="foo"> element. The same pseudo-class works in CSS stylesheets without JavaScript.
Concept
Understanding the :target Selector
Think of :target as “which section does the URL point at right now?”:
page.html#about → #about element matches :target.
page.html (no hash) → nothing matches :target.
$(":target") → any element type with matching id.
$("section:target") → only if the targeted node is a section.
💡
Beginner Tip
Every hash link needs a matching id on the destination element. Without it, the URL changes but :target matches nothing.
Foundation
📝 Syntax
Official jQuery API form (since 1.9):
jQuery
jQuery( ":target" )
// any targeted element:
$( ":target" )
// scoped (official docs example for #foo on a p):
$( "p:target" )
$( "section:target" )
// read current hash:
window.location.hash // "#foo"
// native CSS (standard):
document.querySelector( ":target" )
// CSS stylesheet (no jQuery):
// :target { background: #fef3c7; }
Parameters
No arguments — matches the element whose id equals the URI fragment (without #).
Return value
A jQuery object containing zero or one element — at most one id matches the single hash.
Hash-aware selection vs always-on id lookup vs document root.
:target
:target
Matches URL hash
#id
$("#foo")
Always that id
:root
:root
Document root
CSS
:target { … }
No JS needed
Hands-On
Examples Gallery
Examples follow the official jQuery :target definition for URI fragments. Try the labs with different hash values in the URL bar to see selection change live.
📚 Hash Fragment Basics
Style the element the URL points at.
Example 1 — Highlight :target on Load
When the URL ends with #about, add a highlight class to the targeted element.
jQuery
$( ":target" ).addClass( "is-highlighted" );
// With URI …/page.html#about → #about gets the class
// With no hash → matched set is empty
Click nav links → hash changes → new :target gets .active
Previous section loses highlight
How It Works
CSS :target updates automatically; jQuery needs hashchange (or similar) to re-run class toggles on navigation.
Applications
🚀 Common Use Cases
Documentation TOCs — highlight the section linked from the table of contents.
FAQ accordions — open or emphasize the panel matching #question-3.
Tab-less nav — style section:target as the active panel.
Shareable links — send URLs with hashes so recipients land on the right paragraph.
CSS-only UX — use :target { } for lightbox or reveal patterns.
Analytics — log $( ":target" ).attr( "id" ) on hashchange.
🧠 How jQuery Evaluates :target
1
Read URI fragment
Browser exposes the hash — e.g. #foo from location.hash.
Hash
2
Find id match
Look for an element whose id attribute equals the fragment (without #).
Lookup
3
Apply tag filter
If selector is p:target, keep the match only when the node is a paragraph.
Scope
4
✓
Return collection
Zero or one element — chain .addClass(), read text, or animate scroll.
Important
📝 Notes
Available since jQuery 1.9 — matches URI fragment target element.
Standard CSS — works in querySelector(":target") and stylesheets.
At most one element matches — one hash fragment at a time.
No hash in URL → empty jQuery collection.
Fragment ids must be unique — duplicate ids produce undefined behavior.
Do not confuse with jQuery .target event property or :root document root.
Compatibility
Browser Support
The :target pseudo-class is standard CSS and works in jQuery 1.9+. Native document.querySelector(":target") and CSS :target { } rules are supported in all modern browsers.
✓ jQuery 1.9+ · standard CSS
jQuery :target Selector
URL hash fragment → element with matching id.
100%jQuery + native CSS
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:targetUniversal
Bottom line: Pair anchor links with matching ids; CSS :target works without JavaScript.
Wrap Up
Conclusion
The :target selector matches the element referenced by the URL hash — official docs example: URI #foo and $("p:target") for <p id="foo">.
Use it for deep-link highlights, or pure CSS :target rules when JavaScript is not needed.
Listen for hashchange when toggling jQuery classes
Scope with section:target when needed
Prefer semantic section ids (#pricing, #faq)
❌ Don’t
Confuse :target with $("#id") — hash matters
Expect :target to match without a hash in the URL
Duplicate ids on one page
Forget URL-encoded characters in fragment ids
Assume :target works for query strings — only the hash
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :target
Hash → id match.
5
Core concepts
#01
:target
Hash id
API
0–102
One max
Per hash
Rule
p:03
Scoped
Tag filter
Tip
CSS04
Standard
Native OK
Compare
Δ05
hashchange
Re-query
Demo
❓ Frequently Asked Questions
:target selects the element indicated by the fragment identifier (hash) in the document URI. If the URL is https://example.com/#foo, the element with id="foo" matches. Official jQuery docs example: $("p:target") selects a p element with that id. Available since jQuery 1.9.
$("#foo") always finds the element with id foo regardless of the URL hash. :target matches only when the current URL fragment equals that id — it reflects which section the user navigated to via the hash.
Yes. :target is defined in CSS Selectors and works in native document.querySelector(":target"). jQuery added support in 1.9 to align with the standard pseudo-class.
:root selects the document root element (html in HTML pages). :target selects whichever element id matches the URL hash fragment — often a section deep in the page, not the root.
No element matches :target — the matched set is empty. Once the user clicks a link to #section or the hash changes, the corresponding element becomes :target.
Yes. Listen for the hashchange event (or popstate in some SPA setups), then re-query $(":target") or apply styles. CSS :target rules also update automatically when the fragment changes.
Did you know?
CSS-only lightbox patterns use :target on hidden panels — when the URL becomes #modal, the #modal element matches :target and CSS can display it with :target { display: block; }. jQuery can enhance the same ids with animations, but the pseudo-class works without any script.