CSS Interview Questions

28 Questions
HTML & CSS

HTML & CSS

Web DevelopmentFrontend

Question 26

What is the difference between :nth-child() and :nth-of-type()?

Answer:

The :nth-child() and :nth-of-type() pseudo-classes in CSS are used to select elements based on their position within a parent container. However, they differ in how they determine which elements to target. Here’s a detailed explanation of the differences:

:nth-child()

  • How It Works: The :nth-child() pseudo-class selects elements based on their position among all siblings within a parent element, regardless of the element's type.
  • Syntax: :nth-child(n) where n can be a number, keyword, or formula.
    • Example: :nth-child(2) selects the second child of a parent, whether it’s a div, p, span, or any other element.

Example Usage:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <p>Not an li element</p>
    <li>Item 4</li>
</ul>
li:nth-child(2) {
    color: red;
}
  • Explanation: In this example, li:nth-child(2) selects the second child of the ul element, which is the second <li> element. The third <li> is actually the fourth child because there is a <p> element between the second and third <li>. Therefore, only the second <li> will be styled with red text.

:nth-of-type()

  • How It Works: The :nth-of-type() pseudo-class selects elements based on their position among siblings of the same type (i.e., elements that have the same tag name).
  • Syntax: :nth-of-type(n) where n can be a number, keyword, or formula.
    • Example: :nth-of-type(2) selects the second element of its type (e.g., the second li) within a parent element.

Example Usage:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <p>Not an li element</p>
    <li>Item 4</li>
</ul>
li:nth-of-type(2) {
    color: blue;
}
  • Explanation: In this example, li:nth-of-type(2) selects the second <li> element within the ul, regardless of any other types of elements between the li elements. This means the second <li> will be styled with blue text.

Key Differences Between :nth-child() and :nth-of-type():

  1. Selection Basis:

    • :nth-child(): Targets an element based on its position among all child elements, regardless of the element type.
    • :nth-of-type(): Targets an element based on its position among siblings of the same element type.
  2. Sibling Consideration:

    • :nth-child(): Considers all child elements, including those of different types.
    • :nth-of-type(): Considers only siblings of the same type (same tag name).
  3. Use Case:

    • :nth-child(): Use when you need to select an element based on its position among all siblings, for instance, styling the second child of any type within a container.
    • :nth-of-type(): Use when you need to select an element based on its position among siblings of the same type, for instance, styling the second paragraph in a list of paragraphs.

Example Illustrating Both:

<div>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
    <span>Not a paragraph</span>
    <p>Paragraph 3</p>
    <p>Paragraph 4</p>
</div>
p:nth-child(3) {
    color: red; /* Targets the <span>, not the <p> */
}

p:nth-of-type(3) {
    color: blue; /* Targets the third <p> */
}
  • :nth-child(3): Targets the third child of the div, which is the <span> element. Even though it’s not a paragraph, it is the third child, so it would be selected.
  • :nth-of-type(3): Targets the third <p> element within the div, ignoring the <span>. Only the third paragraph will be styled.

Conclusion

  • :nth-child() is used when you want to select an element based on its position among all child elements of its parent.
  • :nth-of-type() is used when you want to select an element based on its position among siblings of the same type (same tag).

Understanding the difference between these two pseudo-classes helps you write more precise and effective CSS for your web designs.

Recent job openings