CSS :nth-child(n) property


The :nth-child(n) pseudo-class in CSS is used to style elements based on their position within their parent. It allows you to select elements that match a specific pattern, making it a powerful tool for applying styles to elements based on their index.

Syntax

selector:nth-child(n) { /* styles */ }
  • selector: The element or elements you want to target.
  • :nth-child(n): The pseudo-class selects elements based on their position in the parent element.

How It Works

  1. Basic Usage:

    • The n in :nth-child(n) can be a specific number, a keyword, or a formula. It allows you to select elements based on their numerical position among siblings.
    ul li:nth-child(2) { color: red; }
    • In this example, the second <li> element within each <ul> will be styled with red text.
  2. Using Formulas:

    • The :nth-child(n) pseudo-class can also use formulas to target elements in a repeating pattern. The formula is an + b, where a and b are integers, and n is a counter starting from 0.
    div p:nth-child(2n) { background-color: yellow; }
    • This example applies a yellow background to every even <p> element within a <div>, where 2n means every second child starting from the second.
  3. Special Keywords:

    • You can use keywords with :nth-child to target specific patterns.

    • odd: Selects all odd-numbered elements.

      table tr:nth-child(odd) { background-color: lightgray; }
    • even: Selects all even-numbered elements.

      table tr:nth-child(even) { background-color: white; }

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 Child Example</title> <style> ul li:nth-child(1) { color: blue; } ul li:nth-child(3n) { color: green; } p:nth-child(2n) { background-color: yellow; } div p:nth-child(odd) { color: red; } </style> </head> <body> <ul> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li> <li>Fifth item</li> </ul> <div> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> </div> </body> </html>

Explanation:

  • ul li:nth-child(1):

    • Styles the first <li> element within each <ul> with blue color.
  • ul li:nth-child(3n):

    • Styles every third <li> element within each <ul> with green color.
  • p:nth-child(2n):

    • Applies a yellow background to every second <p> element within a <div>.
  • div p:nth-child(odd):

    • Styles every odd-numbered <p> element within a <div> with red color.

Important Points

  1. Indexing Starts at 1:

    • The :nth-child(n) pseudo-class uses a 1-based index. So, :nth-child(1) targets the first child, :nth-child(2) targets the second child, and so on.
  2. Only Works with Sibling Elements:

    • The :nth-child pseudo-class only applies to siblings. It does not apply to elements based on their position in nested structures or descendants.
  3. Patterns and Formulas:

    • The formula an + b allows for powerful and flexible styling patterns. For example, 3n + 1 targets every third child, starting with the first.
  4. Use with Other Pseudo-Classes:

    • You can combine :nth-child with other pseudo-classes like :hover or :first-child for more specific styling.
    ul li:nth-child(2n):hover { background-color: lightblue; }
    • This example applies a light blue background to every even <li> element when hovered over.