(二)CSS选择器

发布时间:2022-06-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了(二)CSS选择器脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

2. 选择器

作用:选择页面上的某一和或者某一类的元素

2.1 基本选择器

  1. 标签选择器:选择一类标签 标签{}

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
        <style>
            /*标签选择器,会选择这个页面上所有的这个标签的元素*/
            h1{
                color: #63005e;
            }
    
            p{
                font-size: 80px;
            }
        </style>
    
    </head>
    <body>
    
    <h1>学Java</h1>
    <h1>学Java</h1>
    <p>听锁子哥说</p>
    
    </body>
    </html>
    
  2. 类选择器 class:选中所有class属性一致的标签,跨标签 .类名{}

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
        <style>
            /* 类选择器的格式 .class的名称{}
            好处, 可以多个标签归类, 是同一个class,可以复用
    
            */
            .hansuo{
                color: red;
            }
            .suozige{
                color: yellow;
    
            }
    
        </style>
    
    </head>
    <body>
    
    <h1 class="hansuo">标题一</h1>
    <h1 class="suozige">标题二</h1>
    <h1 class="hansuo">标题三</h1>
    
    <p class="suozige">P标签</p>
    
    </body>
    </html>
    
    
  3. Id选择器:全局唯一! #id名{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /*id选择器 id必须保证全局唯一
         #id名称{}
         id选择器>class选择器>标签选择器
        */
        #hansuo{
            color: pink;
        }
        .style1{
            color: green;
        }
        .style2{
            color: cyan;
        }
        h1{
            color: gold;
        }


    </style>

</head>
<body>

<h1 id="hansuo">标题一</h1>
<h1 class="style1">标题二</h1>
<h1 class="style1">标题三</h1>
<h1>标题四</h1>
<h1>标题五</h1>

</body>
</html>

优先级: id > class > 标签

2.2 层次选择器

  1. 后代选择器:在某个元素后面 祖爷爷 爷爷 爸爸 你

    /*后代选择器*/
    body p{
    background: red;
    }
    
  2. 子选择器,一代,儿子

    /*子选择器*/
    body>p{
    background: #3cbda6;
    }
    
    
  3. 相邻兄弟选择器 同辈

    /*相邻兄弟选择器,只有一个,相邻(向下)*/
    .active + p{
    background: brown;
    }
    
  4. 通用选择器

    /*通用兄弟选择器,当前选中元素的向下所有兄弟元素*/
    .active~p{
        background: green;
    }
    

2.3 结构伪类选择器

伪类: 条件

p:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

<!--    避免使用class和id选择器-->
    <style>
        /*ul的第一个子元素*/
        ul li:first-child{
            background: blue;
        }
        /*ul的最后一个子元素*/
        ul li:last-child{
            background: red;
        }

        /*选中 p1 :定位到父元素,选择当前的第一个元素
        选中当前p元素的父亲元素,选中父级元素的第一个,便能够切实当前元素才生效!,按顺序
        */
        p:nth-child(1){
            background: green;
        }

        /*选中父元素,下的p元素的第二个,类型*/
        p:nth-of-type(2){
            background: gold;
        }

        /*
        a:hover{
            background: black;
        }
        */

    </style>

</head>
<body>

<!--    <a href="">123123123</a>-->
<!--    <h1>h1</h1>-->
    <p>p1</p>
    <p>p2</p>
    <p>p3</p>
    <ul>
        <li>li1</li>
        <li>li2</li>
        <li>li3</li>
    </ul>

<p class="active">p7</p>
<p>p8</p>
</body>
</html>

(二)CSS选择器

2.4属性选择器(常用)

id + css结合

<!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: #2700ff;
            text-align: center;
            color: gainsboro;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;

        }
        /* 属性名,  属性名 = 属性值(正则)
        = 绝对等于
        *= 包含这个元素
        ^= 以这个开头
        $= 以这个为结尾
        */
        /*存在id属性的元素
        a[]{}
        */
        /*a[id]{*/
        /*    background: yellow;*/
        /*}*/
        /*id = first的元素*/
        /*a[id = first]{*/
        /*    background: green;*/

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

        /*}*/
        a[href$=jpg]{
            background: yellow;
        }




    </style>

</head>
<body>

<p class="demo">
    <a href="https://www.baidu.com" class="links item first" id="first">1</a>
    <a href="https://www.cnblogs.com/qkshhan/" 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="adc.doc"class="links item">9</a>
    <a href="abcd.doc" class="links item last">10</a>
</p>
</body>
</html>

(二)CSS选择器

=
*=
^=
$=

CSS,JS,JQuery,vue都要用

脚本宝典总结

以上是脚本宝典为你收集整理的(二)CSS选择器全部内容,希望文章能够帮你解决(二)CSS选择器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: