CSS层次选择器,结构伪类选择器,属性选择器

1.层次选择器

  1. 后代选择器:在某个元素的后面 祖爷爷 爷爷 爸爸 儿子
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /* 后代选择器*/
        body p{
            background: red;
        }
    </style>
</head>
<body>

    <p>p1</p>
    <p>p1</p>
    <p>p1</p>
    <ul>
        <li>
            <p>p4</p>
        </li>
        <li>
            <p>p5</p>
        </li>
        <li>
            <p>p6</p>
        </li>
    </ul>

</body>
</html>

  1. 子选择器 就一代
/*子选择器*/
        body>p{
            background: #45afff;
        }

  1. 相邻兄弟选择器 同辈
/* 兄弟选择器 下面的一个  */  /*active 为id或class属性都行*/
        .active + p{
            background: red;
        }
  1. 通用选择器
/* 通用选择器  当前选中元素的向下所有兄弟元素 */
        .active~p{
            background: green;
        }

2.结构伪类选择器

伪类: 条件

<!--    避免使用class,id选择器-->
    <style>
        /*ul的第一个子元素*/
        ul  li:first-child{   /*如果是多个ul,则选中的那个加个class,ul.class名 li:..{}即可*/
            background: green;
        }
    /*    ul的最后一个子元素*/
        ul li:last-child{
            background: red;
        }
    /*    选中父元素,下的p元素的第二个,类型(了解)*/
        p:nth-of-type(2){
            background: yellow;
        }

    </style>

3.属性选择器(常用)

id + class结合

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .demo a{
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            background: #45afff;
            text-align: center;
            color: black;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;
        }

        /*  存在id属性的元素    a[]{}  */
        /*1.属性名  2.属性名 = 属性值*/

        /*
        = 绝对等于
        *= 包含
        ^= 以这个开头
        $= 以这个结尾
        */

        /*a[id]{*/
        /*    background: yellow;*/
        /*}*/

        /*id=first的元素*/
        /*a[id=first]{*/
        /*    background: yellow;*/
        /*}*/

        /* class中有links的元素   */
        /*a[class*=links]{*/
        /*    background: yellow;*/
        /*}*/

        /* 选中href中以http开头的元素   */
        /*a[href^=https]{*/
        /*    background: yellow;*/
        /*}*/

        a[href$=pdf]{
            background: yellow;
        }
    </style>
</head>
<body>
    <p class="demo">
        <a href="https://www.baidu.com" class="links item first" id="first">1</a>
        <a href="" class="links item active" target="_blank" title="test">2</a>
        <a href="images/123.html" class="links item">3</a>
        <a href="images/123.png" class="links item">4</a>
        <a href="images/123.jpg" class="links item">5</a>
        <a href="abc" class="links item">6</a>
        <a href="/a.pdf" class="links item">7</a>
        <a href="/abc.pdf" class="links item">8</a>
        <a href="abc.doc" class="links item">9</a>
        <a href="abcd.doc" class="links item last">10</a>
    </p>

</body>
</html>

posted @ 2020-08-29 01:06  LaughTale  阅读(256)  评论(0)    收藏  举报