Understanding the :target Pseudo-Class in CSS
The :target pseudo-class in CSS represents an element whose id matches the fragment identifier (hash) in the current URL. It allows you to style elements when they are the target of a link.
:target applies styles only when the element's id matches the URL hash (e.g., #section1).
It is often used for highlighting sections, creating tabbed interfaces, or simple in-page navigation effects.
Only one element can be targeted at a time since a URL can have only one fragment identifier.
In this example, when you click a link pointing to #section1 or #section2, the corresponding section becomes the target and is highlighted using the :target pseudo-class.
Use :target to highlight or animate elements when navigated via fragment identifiers.
Combine with transitions for smooth visual feedback.
Avoid relying solely on :target for critical functionality, as the effect depends on the URL fragment.
Test across browsers, as some older browsers may have limited support.
You're building a FAQ page where clicking a question expands the answer. You're using anchor links and CSS only — how would you use :target to show the answer when the link is clicked?
A user clicks a link like /page#section2, but nothing changes visually. What are two possible CSS reasons why :target isn't working?
You have a button that links to #modal, and you want the modal to appear when clicked. What CSS rule would you write to show it only when #modal is the target?
Your team built a tabbed interface using :target, but users report that after navigating away and back, the wrong tab stays open. What’s likely happening, and how would you debug it?
You’re using :target to toggle a mobile sidebar, but on iOS Safari, the page scrolls unpredictably when the hash changes. How would you fix this without JavaScript?
A designer wants the :target element to fade in smoothly, but transitions aren’t working. What CSS property is probably missing, and what’s a workaround that still uses :target?
You’re designing a documentation site with hundreds of anchor-linked sections. How would you evaluate whether :target is the right solution versus a JS-based router, considering performance, accessibility, and SEO?
Your component library uses :target for modals, but now you need to support deep linking from external sites. What edge cases arise when users bookmark or share URLs with multiple fragments, and how would you handle them?
A legacy feature relies on :target for navigation, but now you’re migrating to React Router. How would you deprecate the CSS-only approach without breaking bookmarks or analytics tracking?
Your company’s documentation platform has been using :target for 5 years across 20+ microsites. Now you’re consolidating into a single SPA. How would you architect a migration plan that preserves deep links, accessibility, and SEO while phasing out CSS-only targeting?
You’re advising a startup on whether to use :target for their new help center. What long-term maintenance, scalability, and cross-team coordination risks would you highlight, and how would you mitigate them?
A major browser is considering deprecating fragment-based scrolling behavior. How would you assess the impact on your CSS-driven navigation systems, and what architectural changes would you propose to future-proof them?