only-child与only-of-type结构类

久伴 2019年9月22日16:35:44 评论 69
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>only-child</title>
    <style>
        div:only-child {
            color: red;
        }
    </style>
</head>
<body>
<div>1</div>
<p>2</p>
<div>3</div>
<section>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</section>
<section>
    <div>2</div>
</section>
</body>
</html>

匹配父元素下唯一子元素,也就是父元素下面只能包含一个div,包含其他任何一个元素就会失效

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>only-of-type</title>
    <style>
        div:only-of-type {
            color: red;
        }
    </style>
</head>
<body>
<div>1</div>
<p>2</p>
<div>3</div>
<section>
    <div>1</div>
   <h3>2222</h3>

</section>
</body>
</html>

匹配父元素下子元素唯一,唯一和only-child不同的是,父元素下面除了一个div外可以包含其他元素,但是div必须只能一个,有二个div也会失效

久伴

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: