CSS :nth-of-type(n) property


The :nth-of-type(n) pseudo-class in CSS is used to select elements of a specific type (tag name) based on their position among siblings of the same type. Unlike :nth-child(n), which selects elements based on their position relative to all sibling elements regardless of type, :nth-of-type(n) focuses only on elements of a particular type.

Syntax

selector:nth-of-type(n) { /* styles */ }
  • selector: The type of element you want to target (e.g., div, p, li).
  • :nth-of-type(n): The pseudo-class selects elements of the specified type based on their position among siblings of the same type.

How It Works

  1. Basic Usage:

    • You can use :nth-of-type(n) to apply styles to elements based on their numerical position among siblings of the same type.
    p:nth-of-type(2) { color: blue; }
    • In this example, the second <p> element among its sibling <p> elements will be styled with blue text.
  2. Using Formulas:

    • The n in :nth-of-type(n) can be a specific number, a keyword, or a formula, allowing for patterns in styling.
    div:nth-of-type(2n) { background-color: lightgray; }
    • This example applies a light gray background to every even <div> element among its sibling <div> elements.
  3. Special Keywords:

    • odd: Selects all odd-numbered elements of a specific type.

      li:nth-of-type(odd) { color: red; }
    • even: Selects all even-numbered elements of a specific type.

      li:nth-of-type(even) { color: green; }

Examples

HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Nth Of Type Example</title> <style> div:nth-of-type(1) { background-color: yellow; } p:nth-of-type(2n) { color: blue; } li:nth-of-type(odd) { background-color: lightgray; } span:nth-of-type(2) { font-weight: bold; } </style> </head> <body> <div>First div</div> <div>Second div</div> <div>Third div</div> <p>First paragraph</p> <p>Second paragraph</p> <p>Third paragraph</p> <ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul> <span>First span</span> <span>Second span</span> <span>Third span</span> </body> </html>

Explanation:

  • div:nth-of-type(1):

    • Targets the first <div> element among sibling <div> elements. This will have a yellow background.
  • p:nth-of-type(2n):

    • Applies blue text color to every even <p> element among sibling <p> elements.
  • li:nth-of-type(odd):

    • Applies a light gray background to every odd <li> element within its parent <ul>.
  • span:nth-of-type(2):

    • Styles the second <span> element among sibling <span> elements with bold text.

Important Points

  1. Type-Specific:

    • :nth-of-type(n) targets elements based on their type, not their position relative to all siblings. This is useful when you want to apply styles to specific elements of a type while ignoring other types.
  2. Indexing Starts at 1:

    • The n in :nth-of-type(n) is 1-based, meaning :nth-of-type(1) targets the first element of that type.
  3. Compatibility with Other Selectors:

    • :nth-of-type can be combined with other pseudo-classes and selectors for more complex styling.
    ul li:nth-of-type(2n):hover { background-color: yellow; }
    • This example changes the background color to yellow for every even <li> element in a <ul> when hovered over.
  4. Does Not Affect Non-Type Siblings:

    • :nth-of-type only affects elements of the specified type and ignores other sibling elements that are not of that type.