css brother selector+, ~

css brother selector + and ~

The Adjacent sibling selector can select the element immediately after another element, and both have the same parent element.
There are two kinds of + and ~, such as (.h1+p and .h1 ~p). The difference between the two is that + only selects the next p element. And ~ selects all p elements behind.
+:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
h1+p{
    
    
   color: blue; 
}
</style>
<body>
    <div class="father">
        <h1>我是一个标题</h1> 
        <p>我是一个段落</p>
        <p>我是一个段落</p>
        <p>我是一个段落</p>
        <h2>我是一个标题</h2> 
        <p>我是一个段落</p>
        <p>我是一个段落</p>
        <p>我是一个段落</p>
    </div>
</body>
</html>

The effect is as follows:
Insert picture description here
~:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
h1~p{
    
    
   color: blue; 
}
</style>
<body>
    <div class="father">
        <h1>我是一个标题</h1> 
        <p>我是一个段落</p>
        <p>我是一个段落</p>
        <p>我是一个段落</p>
        <h2>我是一个标题</h2> 
        <p>我是一个段落</p>
        <p>我是一个段落</p>
        <p>我是一个段落</p>
    </div>
    <div>
        <p>我是一个段落</p>
        <p>我是一个段落</p>
    </div>
</body>
</html>

The effect is as follows:
Insert picture description here
that is, + means next to each other, and ~ emphasizes all elements. .

Guess you like

Origin blog.csdn.net/w_____w_____/article/details/103557233