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

How can you style alternate table rows without adding extra classes?

Styling Alternate Table Rows Using :nth-child() in CSS

You can style alternate (even or odd) table rows using the :nth-child() pseudo-class in CSS. This method lets you apply alternating background colors or styles without adding extra classes to each row.

Key Points About :nth-child()
  1. 1

    :nth-child(odd) targets all odd-numbered rows (1st, 3rd, 5th, etc.).

  2. 2

    :nth-child(even) targets all even-numbered rows (2nd, 4th, 6th, etc.).

  3. 3

    It works based on the element’s position among its siblings in the DOM.

Example: Styling Alternate Table Rows

In this example, the :nth-child(odd) selector applies a light gray background to odd rows, while :nth-child(even) adds a light blue shade to even rows, making the table easier to read.

Best Practices
  1. 1

    Use alternating row colors to improve table readability.

  2. 2

    Combine with hover effects (e.g., tr:hover) for better interactivity.

  3. 3

    Use :nth-of-type() if your table contains mixed elements like tr and th siblings.

  4. 4

    Avoid hard-coded colors that reduce accessibility contrast.

Difficulty: 3/10
Topics: nth-child selector, CSS specificity, dynamic row styling

Scenario Questions

0-2 years experience
  1. 1

    How would you style every other row in a static HTML table to have a light gray background without adding any classes or IDs?

  2. 2

    What happens if you use nth-child(2n) instead of nth-child(even)? Does it behave the same, and why?

2-5 years experience
  1. 1

    A table row background alternates correctly on initial load, but after filtering rows with JavaScript, the styling gets out of sync—how do you debug and fix this?

  2. 2

    Your team’s table component uses inline styles for row colors, but the design system now requires alternating backgrounds. How would you refactor this without breaking existing functionality or requiring markup changes?

5-8 years experience
  1. 1

    You’re optimizing a data-heavy table with 10,000+ rows that uses nth-child for alternating styles—how would you evaluate performance impact, and would you consider a different approach?

  2. 2

    In a legacy app, alternating row styles are implemented with JS classes, but it’s causing reflows on every render. How would you migrate to pure CSS while maintaining accessibility and supporting older browsers?

8+ years experience
  1. 1

    You’re designing a cross-team table component library used across 50+ products—how do you ensure alternating row styling is maintainable, themeable, and doesn’t conflict with user-customized CSS or third-party plugins?

  2. 2

    A legacy system uses inline styles for row alternation, and migrating to CSS selectors risks breaking analytics or accessibility tools that rely on class names. How would you architect a backward-compatible solution with zero-downtime rollout?

Follow-up Questions

  • What if the table rows are added dynamically via JavaScript after page load?
  • How would you handle this if the table had nested rows or headers that shouldn't be styled?
  • Could this break if the table structure changes to include <thead> or <tfoot>?