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
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.
- The
Using Formulas:
- The
:nth-child(n)
pseudo-class can also use formulas to target elements in a repeating pattern. The formula isan + b
, wherea
andb
are integers, andn
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>
, where2n
means every second child starting from the second.
- The
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.
- Styles the first
ul li:nth-child(3n)
:- Styles every third
<li>
element within each<ul>
with green color.
- Styles every third
p:nth-child(2n)
:- Applies a yellow background to every second
<p>
element within a<div>
.
- Applies a yellow background to every second
div p:nth-child(odd)
:- Styles every odd-numbered
<p>
element within a<div>
with red color.
- Styles every odd-numbered
Important Points
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.
- The
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.
- The
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.
- The formula
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.
- You can combine