关于伪类:first-child和:first-of-type

关于伪类:first-child和:first-of-type

:first-child是父元素下面的第一个子元素的位置

first-of-type是父元素下一个类型元素的第一个

h1:first-child

<!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>Document</title>
    <style>
        h1:first-child{
            background: red;
        }
    </style>
</head>

<body>
    <h1>h1-1</h1>
    <div>
        <h1>h1-1</h1>
        <h1>h1-2</h1>
        <p>p1-1</p>
        <p>p1-2</p>
    </div>
    <div>
        <p>p1-1</p>
        <p>p1-2</p>
        <h1>h1-1</h1>
        <h1>h1-2</h1>
    </div>
    <h1>h1-2</h1>

</body>

</html>

<!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>Document</title>
    <style>
        p:first-of-type{
            background: greenyellow;
        }md
    </style>
</head>

<body>
    <h1>h1-1</h1>
    <p>p1-1</p>
    <div>
        <h1>h1-1</h1>
        <h1>h1-2</h1>
        <p>p1-1</p>
        <p>p1-2</p>
    </div>
    <div>
        <p>p1-1</p>
        <p>p1-2</p>
        <h1>h1-1</h1>
        <h1>h1-2</h1>
    </div>
    <h1>h1-2</h1>

</body>

</html>

posted @ 2023-05-12 09:34  学习让我充实  阅读(35)  评论(0)    收藏  举报