:target selects the element indicated by the fragment identifier (hash) in the document URI. Official docs: for https://example.com/#foo, $("p:target") selects the <p id="foo"> element.
Remember
URL: https://example.com/page#foo
$(":target") → element with id="foo"
$("p:target") → only if that element is a <p> (official style)
$("#foo") → always finds #foo (hash does not matter)
// No hash → $(":target").length === 0
Give sections stable ids, link with #id, then style or read the current :target.
Add an id to the element you want deep-linked (for example id="intro").
Link to it with href="#intro" (or set location.hash).
Query with $(":target") or a scoped form like $("section:target").
Re-run the query on hashchange if you highlight with jQuery (CSS :target updates alone).
Handle the empty case when the URL has no fragment.
Piece
Required?
Role
URL fragment
Yes (for a match)
#id in the address bar
Matching id
Yes
Element whose id equals the fragment
Tag prefix
Optional
p:target only matches if the target is a p
Return value
—
0 or 1 element (empty if no hash / no id)
Pseudocode
Pseudocode
hash = URL fragment without "#"
if hash is empty:
return empty set
target = element whose id == hash
if not found:
return empty set
return jQuery(target)
// Optional: filter by tag → p:target, section:target, …
Reference
Cheat Sheet
Keep these facts handy while working with URL fragments.
Topic
Detail
Syntax
$(":target") / $("p:target")
Since
jQuery 1.9 · standard CSS
Matches
Element whose id equals the URL hash
Count
0 or 1
vs $("#id")
Id always; :target only when hash matches
No hash
Empty collection
Updates
On navigation / hashchange
Native CSS
document.querySelector(":target")
Try it
Live Preview
Jump to a section id below. The preview reads $(":target") for this page’s current hash.
Check length.0 means do not call methods that need a node.
Message. Tell the user (or skip highlight logic).
Later. When a hash appears, the same selector starts matching.
Edge Cases & Pitfalls
Check these before building deep-link UI with :target.
empty
No hash → empty set
$(":target").length is 0 until a fragment is present and matches an id.
id
Missing id = no match
A hash like #missing matches nothing if no element has that id.
tag
Tag filter can reject a valid target
$("p:target") fails if the hashed element is a div. Use bare :target when any element type is fine.
version
Needs jQuery 1.9+
Older builds lack :target. Fall back to reading location.hash and $("#" + id).
Compatibility
Browser Support
The :target pseudo-class is standard CSS and has been in jQuery since 1.9. It works in native querySelector(":target") in all modern browsers.
✓ CSS · jQuery 1.9+
jQuery :target Selector
Supported in jQuery 1.9+ and every modern browser. Native: document.querySelector(":target") returns the element matching location.hash.
100%Standard CSS selector
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: Safe on current jQuery builds. Pair with hashchange (or CSS :target rules) for live deep-link highlighting.
Analysis
Performance Notes
Approach
Cost
Best for
$(":target")
Cheap
Reading the active fragment target
document.querySelector(":target")
Cheap
Native CSS without jQuery
$("#" + location.hash.slice(1))
Cheap
Fallback / older jQuery
CSS :target { … }
Cheapest
Highlighting without JS
Because :target is standard CSS, jQuery can use the browser’s native engine. Cost is negligible — you resolve at most one element.
Remember
Key Takeaways
Role: select the element whose id matches the URL hash.
Not id alone:$("#foo") always matches; :target only when the hash is #foo.
Standard: works in native CSS; jQuery support since 1.9.
Live UI: re-query on hashchange, or style with CSS :target.
One line:$(":target") is the element named by the URL hash — empty with no fragment, perfect for deep-link highlighting.
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 example: $("p:target") selects a p with that id. Available since jQuery 1.9.
$("#foo") always finds the element with id foo. :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 (html in HTML). :target selects whichever element id matches the URL hash — often a section deep in the page, not the root.
No element matches :target — the matched set is empty. After the user follows a #section link or the hash changes, that element becomes :target.
Yes. Listen for hashchange, then re-query $(":target") or update styles. CSS :target rules also update automatically when the fragment changes.