博客园不常在线

有问题联系微信

微信号



微信公众号



前端系列:CSS选择器(标签、ID、类、通配符、后代、子元素、并集、伪类)

一.标签选择器

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h1 {
            color: red;
        }
        h2{
            color: green;
        }
    </style>
</head>

<body>
    <h1>Hello World!</h1>
    <h1>Hello World!</h1>
    <h2>Hello !</h2>
    <h2>Hello !</h2>
</body>

</html>

二.类选择器

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            color: green;
        }

        .box1 {
            font-size: 26px;
        }
    </style>
</head>

<body>
    <div class="box box1">Hello World!</div>
    <span class="box">Hello World!</span>
</body>

</html>

三.ID选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
#box{
    color: green;
}
    </style>
</head>
<body>
    <div id="box">Hello World!</div>
</body>
</html>

四.通配符选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            color: red;
        }
    </style>
</head>
<body>
    <div>Hello World</div>
</body>
</html>

五.后代选择器

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul li {
            color: red;
        }

        ul li a {
            color: green;
        }

        .nav li {
            color: yellow;
        }
    </style>
</head>

<body>
    <ul>
        <li>ul li</li>
        <li>ul li</li>
        <li>ul li</li>
        <li>ul li</li>
        <li>ul li <a href="">a</a></li>
    </ul>

    <ol>
        <li>ol li</li>
        <li>ol li</li>
        <li>ol li</li>
        <li>ol li</li>
        <li>ol li</li>
    </ol>

    <ul class="nav">
        <li>nav li </li>
        <li>nav li </li>
        <li>nav li </li>
        <li>nav li </li>
        <li>nav li </li>
    </ul>
</body>

</html>

六.子元素选择器

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav>a {
            color: red;
        }
    </style>
</head>

<body>
    <div class="nav">
        <a href="#">Hello World</a>
        <p>
            <a href="#">Hello World</a>
        </p>
    </div>
</body>

</html>

 

posted @ 2020-11-08 14:23  Code技术分享  阅读(373)  评论(0编辑  收藏  举报