Questions
18 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
18 / 50

What does the :target pseudo-class represent?

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.

Key Points About :target
  1. 1

    :target applies styles only when the element's id matches the URL hash (e.g., #section1).

  2. 2

    It is often used for highlighting sections, creating tabbed interfaces, or simple in-page navigation effects.

  3. 3

    Only one element can be targeted at a time since a URL can have only one fragment identifier.

Example: Using :target

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.

Best Practices
  1. 1

    Use :target to highlight or animate elements when navigated via fragment identifiers.

  2. 2

    Combine with transitions for smooth visual feedback.

  3. 3

    Avoid relying solely on :target for critical functionality, as the effect depends on the URL fragment.

  4. 4

    Test across browsers, as some older browsers may have limited support.

Difficulty: 3/10
Topics: CSS pseudo-classes, anchor link targeting, UI state styling

Scenario Questions

0-2 years experience
  1. 1

    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?

  2. 2

    A user clicks a link like /page#section2, but nothing changes visually. What are two possible CSS reasons why :target isn't working?

  3. 3

    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?

2-5 years experience
  1. 1

    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?

  2. 2

    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?

  3. 3

    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?

5-8 years experience
  1. 1

    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?

  2. 2

    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?

  3. 3

    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?

8+ years experience
  1. 1

    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?

  2. 2

    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?

  3. 3

    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?

Follow-up Questions

  • How would you prevent layout jank when :target triggers a sudden style change?
  • What happens if multiple elements have the same ID and one is targeted?
  • Can you use :target to style a parent element? Why or why not?