Course

CSS nth-child Selector: Selecting Elements with Precision

The CSS nth-child selector targets the nth child element of a parent element based on a specific position, keyword, or formula.

How to Use the CSS nth-child Selector

The CSS nth-child selector, written as :nth-child(n), targets the nth child of a parent element. n can be a specific position, a keyword like odd and even, or an equation like 2n+1.

css
element:nth-child(n) { /* styles go here */ }
  • element: The element to target as the nth child element of a parent element.
  • :nth-child: The selector to target the nth child element of a parent element.
  • n: A position, keyword, or formula to indicate which instance of the element to select.

Basic Usage

css
/* Select every second paragraph in a parent */ p:nth-child(2n) { color: blue; } /* Select every third list item */ li:nth-child(3n) { background: yellow; } /* Select the first element */ div:nth-child(1) { border: 1px solid black; }

When to Use the CSS Selector nth-child

The nth-child selector is useful to style elements based on their position within a parent.

Styling Alternating Rows in Tables

You can use the nth-child selector to style alternating rows in a table for better readability.

css
table tr:nth-child(odd) { background-color: #f2f2f2; } table tr:nth-child(even) { background-color: #ffffff; }

Highlighting List Items

To create visual distinction among list items, you can use nth-child to highlight every third item.

css
ul li:nth-child(3n) { font-weight: bold; color: red; }

Customizing Form Fields

With nth-child, you can also style specific form fields differently, which can help guide user input more effectively.

css
form input:nth-child(4) { border: 2px solid green; }

Examples of Using nth-child in CSS

Responsive Design

In responsive web design, nth-child can help change the layout or appearance of elements based on screen size.

css
@media (min-width: 600px) { .item:nth-child(odd) { float: left; width: 45%; } .item:nth-child(even) { float: right; width: 45%; } }

Theming Webpages

Web pages that need a unique look for different sections often use nth-child to apply varied styles.

css
section:nth-child(odd) { background-color: #e0f7fa; } section:nth-child(even) { background-color: #ffe0b2; }

Dynamic Content Styling

Websites with dynamic content, such as blogs, use nth-child to adapt styling as new posts are added.

css
.article:nth-child(even) { margin-bottom: 20px; } .article:nth-child(odd) { margin-bottom: 30px; }

Learn More About CSS Child Selectors

Combining with Other Pseudo-classes

The nth-child selector works well with other pseudo-classes and pseudo-elements, enhancing its flexibility.

css
li:nth-child(2n):hover { background: lightgreen; }

Negative Values in nth-child

You can use negative values to target elements in reverse order. This is useful for targeting elements from the end of a parent element’s children.

css
li:nth-child(-n+3) { color: purple; }

Complex Formulas in nth-child

The nth-child selector supports more complex formulas for advanced selection. This allows for precise targeting of elements based on various patterns.

css
/* Select every 4th element starting from the 3rd */ li:nth-child(4n+3) { font-size: 1.2em; }

CSS nth-of-type Selector

The nth-of-type selector targets elements based on their position among sibling elements of the same type.

css
p:nth-of-type(2) { font-style: italic; }

This example styles the second paragraph among its siblings to be italic.

CSS last-child Selector

The last-child selector targets the last child element within a parent. This is helpful for applying styles to the final element in a list or group.

css
ul li:last-child { font-weight: bold; }

This example makes the last list item bold.

CSS only-child Selector

The only-child selector targets an element that is the only child of its parent. This ensures that the styles apply only when an element is the sole child.

css
div p:only-child { text-align: center; }

This example centers the text of a paragraph if it is the only child within a div.