Questions
3 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?
03 / 50

What is the syntax of a pseudo-class in CSS?

Difficulty: 3/10
pseudo-class syntax, selector specificity, state styling

CSS Pseudo-Class Syntax

In CSS, a pseudo-class is written by appending a single colon : followed by the pseudo-class name to a selector. It targets elements based on their state, position, or relationship in the DOM.

Syntax Rules
  1. 1

    Single colon : precedes the pseudo-class (e.g., :hover, :first-child).

  2. 2

    Placed directly after the selector without any space (e.g., button:hover).

  3. 3

    Can be combined with other selectors and pseudo-classes (e.g., ul li:first-child:hover).

  4. 4

    Cannot create new elements; only applies styles conditionally to existing elements.

Example: Pseudo-Class Syntax

This example demonstrates the syntax of pseudo-classes to style elements dynamically based on hover, focus, and position in a list.

Best Practices
  1. 1

    Use pseudo-classes to style dynamic states without extra HTML classes.

  2. 2

    Combine pseudo-classes with pseudo-elements (::before, ::after) for richer styling.

  3. 3

    Ensure that hover or focus styles do not convey essential information inaccessible to keyboard users.

  4. 4

    Test pseudo-class behavior across browsers for consistency.

Scenario Questions

0-2 years experience

  1. 1We need a button that changes background when the user hovers over it. Show me the CSS selector you’d write and explain the pseudo‑class syntax you used.
  2. 2If you want to style only the first item in an unordered list, which pseudo‑class would you pick and how would you write the selector?
  3. 3What happens if you accidentally omit the colon before a pseudo‑class in a selector? How would the browser interpret it?

2-5 years experience

  1. 1Our navigation menu adds an 'active' class to the current link, but the design team prefers a CSS‑only solution. How would you use a pseudo‑class to highlight the active link, and what syntax pitfalls might cause it to break in older browsers?
  2. 2A teammate reports that ':focus-visible' styles aren’t applying on Chrome. Walk me through how you’d check the selector syntax and debug the issue.
  3. 3You need to style every odd row in a table using a pseudo‑class. Write the selector and discuss any specificity or ordering concerns with existing table styles.

5-8 years experience

  1. 1Our design system relies on a utility‑first framework, but we need a custom ':loading' pseudo‑class that applies several properties. How would you structure the selector syntax to keep it maintainable and avoid specificity wars across the component library?
  2. 2We’re refactoring a legacy codebase that heavily uses ':nth-child' for layout. What performance implications does that pseudo‑class syntax have, and how would you redesign the selectors for better rendering at scale?
  3. 3When supporting both light and dark themes, we want to combine ':root' custom properties with ':hover' pseudo‑classes. Explain how you’d write those selectors and any syntax pitfalls that could affect theme switching.

8+ years experience

  1. 1Our organization is moving from CSS‑in‑JS to pure CSS and must decide whether interactive states stay in JavaScript or shift to pseudo‑classes. Discuss the architectural trade‑offs, focusing on pseudo‑class syntax, maintainability, and cross‑team consistency.
  2. 2A cross‑functional team wants a company‑wide style guide for pseudo‑class usage to reduce duplication. How would you define the correct syntax, naming conventions, and fallback strategies for browsers lacking support?
  3. 3We have a global ':focus' rule for accessibility, but some components override it with higher specificity. Propose a strategy using modern selector syntax (e.g., :where, :is) to enforce the base styles without breaking existing overrides.

Follow-up Questions

  • How would you verify that your pseudo‑class selector works consistently across major browsers?
  • In what ways does a pseudo‑class affect CSS specificity compared to a regular class selector?
  • Can you think of a scenario where using a pseudo‑class might introduce an unexpected layout or interaction issue?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.