CSS selects the element before the current element
CSS Select the Previous Element

When designing web pages, we often need to use CSS selectors to select the element before a certain element to achieve specific styling effects. This article will discuss in detail how to use CSS selectors to select the element before the current one.
Why do we need to select the element before the current one?
In web design, we sometimes need to set styles or perform other operations on the element before a certain element. For example, when adjusting the style of a paragraph, we may need to select the heading element before the paragraph to achieve a specific layout effect.
CSS selectors are powerful tools that help us precisely select elements in the DOM, enabling us to achieve a variety of styling effects. By selecting the element immediately preceding the current element, we can more flexibly control the layout and style of the page.
CSS Selectors: Selecting the Element Previous to the Current Element
In CSS, there are several selectors that can be used to select the element immediately preceding the current element. Below we will introduce several common methods:
1. Using the Adjacent Sibling Selector
The adjacent sibling selector selects the adjacent sibling elements of an element. To select the element immediately preceding the current element, use the + symbol to concatenate two selectors, as shown below:
p + h1 {
/* Style settings */
}
In the code above, p + h1 selects all <h1> elements immediately following the <h1> element. This effectively selects the element immediately preceding the current element.
Sample Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Selecting Previous Element</title>
<style>
p + h1 {
color: red;
}
</style>
</head>
<body>
<h1>Title</h1>
<p>Paragraph 1</p>
<h1>Another Title</h1>
<p>Paragraph 2</p>
</body>
</html>
Running Results
In the example code above, we select the title element after each paragraph and set the color to red. Therefore, the first title element “Title” will turn red, while the second title element “Another Title” will turn red. “Title” will not be affected.
2. Use the General Sibling Selector
The general sibling selector is similar to the adjacent sibling selector, but it selects all siblings after the current element. When we need to select the previous element and all previous siblings of the current element, we can use the ~ symbol to connect the selectors of two elements, as shown below:
p ~ h1 {
/* Style settings */
}
In the above code, p ~ h1 selects all <h1> elements following the <p> element. This selects the previous element and all preceding sibling elements of the current element.
Sample code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Selecting Previous Element</title>
<style>
p ~ h1 {
font-weight: bold;
}
</style>
</head>
<body>
<h1>Title</h1>
<p>Paragraph 1
Paragraph 2
<h1>Title 2</h1>
</body>
</html>
Running Results
In the example code above, we select the title element following each paragraph and set the font to bold. Therefore, both title elements, “Title” and “Title 2,” will be set to bold.
Summary
Through this introduction, we learned how to use CSS selectors to select the previous element of the current element, including adjacent sibling selectors and universal sibling selectors. These selectors can help us more flexibly control the style and layout of the page, making web design more colorful.