CSS Previous Sibling
CSS Previous Sibling Element
In CSS, we often use pseudo-classes to select different states or specific positions of elements. The :nth-child
pseudo-class is used to select a sibling of an element, but what if we want to select the previous sibling?
Why might we need to select the previous sibling?
In some layout designs, we may need to modify the style or perform other operations on the previous sibling of an element. Being able to select the previous sibling is very convenient. For example, when styling a table, we might want to change the style of the previous row when the mouse hovers over it.
Use :nth-child
Unable to Select Previous Sibling
In CSS, the :nth-child
pseudo-class is used to select a child element at a specific position, but it cannot directly select the previous sibling element. For example, let’s say we have the following HTML structure:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
If we want to select the previous sibling element of Item 3
, Item 2
, we cannot directly use :nth-child
to achieve this.
Using the General Sibling Combinator to Select the Previous Sibling Element
For the previous sibling element, we can use the General Sibling Combinator in CSS. It represents all sibling elements after a given element under the same parent element.
The syntax for the General Sibling Combinator is ~
. For example:
selector1 ~ selector2 {
/* styles */
}
Here, selector1
is the reference element, and selector2
after ~
refers to all sibling elements after selector1
.
Considering the previous example, if we want to select the previous sibling element, Item 2
, we can write it like this:
li:nth-child(3) ~ li:nth-child(2) {
/* styles */
}
This CSS code selects the second element of all sibling li
elements after the third li
element.
Using the Adjacent Sibling Selector to Select the Previous Sibling Element
In addition to the General Sibling Combinator, we can also use the Adjacent Sibling Selector to select the previous sibling element.
The syntax of the Adjacent Sibling Selector is +
, for example:
selector1 + selector2 {
/* styles */
}
This selector selects all selector2
elements that immediately follow selector1
. Using the previous example, if we want to select the previous sibling element, Item 2
, we can write it like this:
li:nth-child(3) + li {
/* styles */
}
This CSS code selects the next li
element immediately following the third li
element.
Sample Code and Effect Demonstration
Let’s use a simple example to demonstrate how to select the previous sibling element. Consider the following HTML structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Previous Sibling Element</title>
<style>
li:nth-child(3) ~ li:nth-child(2) {
color: red;
}
li:nth-child(3) + li {
font-weight: bold;
}
</style>
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</body>
</html>
In the above code, we use li:nth-child(3) ~ li:nth-child(2)
to select the second li
element after the third li
element Element and set its text color to red. At the same time, we use li:nth-child(3) + li
to select the next li
element after the third li
element and set its font to bold.
Open this HTML file in a browser and you will see that the second li
element after the third li
element turns red, and the next li
element after the third li
element turns bold.
This is how to select the previous sibling element through CSS.
Summary
In CSS, although we cannot directly select the previous sibling element, we can achieve this function through the General Sibling Combinator and Adjacent Sibling Selector. By combining reasonable selectors, we can easily modify the style of the previous sibling element, thereby achieving more flexible page layout and design.