:target
Widely availableSince 201597% of users
Matches the element referenced by a URL fragment. Highlights the element when directly linked to.
What's modern
From web-features, MDN's browser data and our snippets, checked dailyClassicIt has worked everywhere since July 2015.
See how modern your own CSS is.Check your CSS →
Quick example
:target { background-color: yellow; }
:target-within { outline: 2px solid blue; }Examples
Highlight the targeted section
section:target {
background-color: #ffffcc;
border-left: 4px solid #ff9800;
}
/* Visited via #section-id in URL */Highlight the section that's linked via URL fragment, drawing attention to the target.
Show/hide panels based on URL fragment
.panel { display: none; }
.panel:target { display: block; }
/* Panels show only if URL contains #panel-id */Use :target to build bookmark-able accordion or tabbed interfaces without JavaScript.
Scroll to and highlight in one step
#search-result:target {
scroll-margin-top: 5rem;
background-color: #e8f5e9;
border: 2px solid green;
}Browser scrolls to the target and :target styling makes it stand out immediately.
Common pitfalls
- :target only works with URL fragments:target matches elements whose id matches the URL hash. If there's no #fragment in the URL, :target matches nothing.
- Styles reset when navigating awayWhen the user clicks to a different fragment, the old :target styles remove instantly and new ones apply. This can feel jarring; consider transition: background-color 0.3s.
- Conflicts with JavaScript routingIf your app uses JavaScript to change hash without page reload, ensure your JS triggers the same visual changes as :target would.
:target browser support
BaselineWidely availableThis feature is well established and works across many devices and browser versions. It has been available across browsers since 2015.Safe to use without fallbacks.
Usage97%of people can use this3% use a browser without it.
Browsers
1+
1+
1.3+
12+Mobile
18+Android
4+Android
2+iOS