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

How does the :root pseudo-class differ from the html selector?

Understanding the :root Pseudo-Class in CSS

The :root pseudo-class in CSS represents the highest-level element in the document tree, which is usually the <html> element. While it often selects the same element as the html selector, there are important differences.

Key Differences Between :root and html Selector
  1. 1

    :root is a pseudo-class that specifically targets the root element of the document, which is useful for defining global CSS variables (--variable-name).

  2. 2

    html is a type selector that selects all <html> elements. In standard HTML documents, there is only one, so it often overlaps with :root.

  3. 3

    :root has higher specificity than the html selector, so rules defined with :root can override those on html if both are applied to the same properties.

  4. 4

    :root is particularly useful in SVG documents or XML documents where the root element may not be <html>.

Example: Using :root vs html

In this example, the :root pseudo-class is used to define a global CSS variable and set a font size. While the html selector could also set font size, :root is preferred for defining variables and ensures higher specificity.

Best Practices
  1. 1

    Use :root for defining CSS variables and global styles that may need to be referenced throughout the document.

  2. 2

    Use the html selector for general styling of the HTML element when variables are not involved.

  3. 3

    Understand that :root has higher specificity than html, so it can override html styles if necessary.

  4. 4

    Test styles in different document types (HTML, SVG) to ensure :root behaves as expected.

Difficulty: 3/10
Topics: CSS custom properties, selector specificity, scope of root vs html

Scenario Questions

0-2 years experience
  1. 1

    You're trying to set a theme color using --primary-color, but it's not working when you define it on html — what's the most likely reason?

  2. 2

    How would you fix a situation where a CSS variable defined in html isn't being picked up by a child component?

  3. 3

    If you write both html { --color: red; } and :root { --color: blue; }, which one wins and why?

2-5 years experience
  1. 1

    A designer reports that theme colors are inconsistent across pages — you notice some components use html and others use :root for variables. How do you debug and fix this?

  2. 2

    Your team's CSS library uses html for global variables, but now you're adding a third-party widget that styles html itself. What could break, and how would you resolve it?

  3. 3

    Why might a CSS variable defined on html stop working after adding a CSS reset that targets html with !important?

5-8 years experience
  1. 1

    You're designing a theming system for a large UI library — why would you choose :root over html for global variables, and what edge cases might you encounter with shadow DOM or iframes?

  2. 2

    In a micro-frontend architecture, multiple teams define CSS variables on html. How does using :root help avoid conflicts, and what other strategies would you recommend?

  3. 3

    How would you structure a CSS architecture to support dynamic theme switching at runtime using :root, and what performance implications should you consider?

8+ years experience
  1. 1

    You're migrating a legacy codebase where global styles are defined on html across 50+ micro-frontends. What’s your strategy to safely transition to :root without breaking existing themes?

  2. 2

    How would you design a cross-team CSS standard for a 1000+ engineer org that enforces consistent use of :root for theme variables, and how do you handle legacy tooling that auto-generates html selectors?

  3. 3

    When building a CSS-in-JS framework that needs to interoperate with traditional CSS, how do you architect the runtime to ensure :root-based variables are correctly injected and prioritized across shadow boundaries and iframes?

Follow-up Questions

  • What happens if you define a custom property on html instead of :root?
  • Can you override a :root variable with an html selector? Why or why not?
  • Why do most CSS frameworks prefer :root for theme variables?