CSS third child element
CSS Third-Child
In CSS, we often need to select an element’s children for styling. Sometimes we need to select the first child, sometimes the last, and sometimes the third. This article will detail how to use CSS selectors to select the third child and provide relevant code examples.
Selecting the third child element with the :nth-child() selector
In CSS, we can use the :nth-child() selector to select child elements of an element. The selector syntax is :nth-child(n), where n represents the position of the child element to be selected. If we want to select the third child, we can set n to 3. Here is a simple example code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3) {
color: red;
}
</style>
</head>
<body>
<div>First child</div>
<div>Second child</div>
<div>Third child</div>
<div>Fourth child</div>
</body>
</html>
Output:
In the example above, we use the :nth-child(3) selector to select the third div element and set its text color to red. When we run this code, the text color of the third child element will change to red.
Selecting a Third Child of a Different Type
In addition to selecting third child elements of the same type, we can also select third child elements of different types. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3) {
color: red;
}
p:nth-child(3) {
color: blue;
}
</style>
</head>
<body>
<div>first div element</div>
<p>first p element</p>
<div>second div element</div>
<p>second p element</p>
<div>third div element</div>
<p>third p element</p>
</body>
</html>
Output:
In the example above, we select the third div element and the third p element and set their text color to red and blue, respectively. When we run this code, the text color of the third div element will change to red, and the text color of the third p element will change to blue.
Select the third child element of a specific class
In addition to selecting the third child element of a specific type, we can also select the third child element with a specific class. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3).special {
color: green;
}
</style>
</head>
<body>
<div>First div element</div>
<div class="special">Second div element</div>
<div>Third div element</div>
<div class="special">Fourth div element</div>
</body>
</html>
Output:
In the example above, we select the third div element with the class “special” and set its text color to green. When we run this code, the text color of the third div element with the class “special” will change to green.
Selecting the Third Child of an Odd-Numbered Element
Sometimes we may need to select the third child element of an odd-numbered element. We can use the :nth-child(odd) selector to select the odd-numbered child element. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(odd) {
color: purple;
}
</style>
</head>
<body>
<div>First div element</div>
<div>Second div element</div>
<div>Third div element</div>
<div>Fourth div element</div>
<div>Fifth div element</div>
</body>
</html>
Output:
In the example above, we use the :nth-child(odd) selector to select the odd-numbered div elements and set their text color to purple. When we run this code, the text color of the first, third, and fifth div elements will change to purple.
Select the third child element in the even position
Similarly, we can also select the third child element in the even position. We can use the :nth-child(even) selector to select even-numbered child elements. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(even) {
color: orange;
}
</style>
</head>
<body>
<div>first div element</div>
<div>second div element</div>
<div>third div element</div>
<div>fourth div element</div>
<div>fifth div element</div>
</body>
</html>
Output:
In the example above, we use the :nth-child(even) selector to select even-numbered div elements and set their text color to orange. When we run this code, the text color of the second and fourth div elements will change to orange.
Selecting Children of the Third Child
Sometimes we may need to select children of the third child. We can achieve this by nesting the :nth-child() selector. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3) span {
color: brown;
}
</style>
</head>
<body>
<div>
<span>First span element</span>
</div>
<div>
<span>Second span element</span>
</div>
<div>
<span>Third span element</span>
</div>
<div>
<span>Fourth span element</span>
</div>
</body>
</html>
Output:
In the example above, we select the span child element of the third div element and set its text color to brown. When we run this code, the text color of the span child element of the third div element will change to brown.
Selecting Adjacent Elements of the Third Child
Sometimes we may need to select adjacent elements of the third child element. We can use the adjacent sibling selector “+” with the :nth-child() selector to achieve this. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3) + div {
color: teal;
}
</style>
</head>
<body>
<div>first div element</div>
<div>second div element</div>
<div>third div element</div>
<div>fourth div element</div>
</body>
</html>
Output:
In the example above, we select the div element adjacent to the third div element and set its text color to cyan. When we run this code, the text color of the fourth div element will change to cyan.
Selecting Subsequent Elements of the Third-Child Element
Sometimes you may need to select subsequent elements of the third child element. You can use the universal sibling selector “~” with the :nth-child() selector to achieve this. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3) ~ div {
color: pink;
}
</style>
</head>
<body>
<div>first div element</div>
<div>second div element</div>
<div>third div element</div>
<div>fourth div element</div>
<div>fifth div element</div>
</body>
</html>
Output:
In the example above, we select the div elements following the third div element and set their text color to pink. When we run this code, the text color of the fourth and fifth div elements will change to pink.
Selecting the Parent of the Third Child
Sometimes we may need to select the parent of the third child. We can use the parent selector “!” with the :nth-child() selector to achieve this. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3):parent {
background-color: lightblue;
}
</style>
</head>
<body>
<div>
<div>First child</div>
</div>
<div>
<div>Second child</div>
</div>
<div>
<div>Third child</div>
</div>
<div>
<div>Fourth child</div>
</div>
</body>
</html>
Output:
In the example above, we select the parent div of the third child and set its background color to light blue. When we run this code, the background color of the parent div of the third child will change to light blue.
Selecting Siblings of the Third Child
Sometimes we may need to select the siblings of the third child. We can use the “|” selector with the :nth-child() selector to achieve this. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
div:nth-child(3) | div {
font-weight: bold;
}
</style>
</head>
<body>
<div>first div element</div>
<div>second div element</div>
<div>third div element</div>
<div>fourth div element</div>
</body>
</html>
Output:
In the example above, we select the sibling div elements of the third div element and make their font bold. When we run this code, the fonts of the second and fourth div elements will become bold.