【CSS】属性选择器

存否和值选择器

选择器 示例 描述
[attr] a[title] 匹配带有一个名为attr的属性的元素——方括号里的值。
[attr=value] a[href=“https://example.com”] 匹配带有一个名为attr的属性的元素,其值正为value——引号中的字符串。
[attr~=value] p[class~=“special”] 匹配带有一个名为attr的属性的元素,其值正为value,或者匹配带有一个attr属性的元素,其值有一个或者更多,至少有一个和value匹配。注意,在一列中的好几个值,是用空格隔开的。
[attr 竖线 =value] div[lang 竖线=“zh”] 匹配带有一个名为attr的属性的元素,其值可正为value,或者开始为value,后面紧随着一个连字符。

1. [attr] 匹配带有一个名为attr的属性的元素——方括号里的值。

<html>
    <head>
        <style type="text/css">
            a[href] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2>[attr]</h2>

        <p><a name="anchor">不存在href属性</p>
        <p><a href="#">存在属性href</p>
    </body>
</html>

在这里插入图片描述


2.[attr=value] 匹配带有一个名为attr的属性的元素,其值正为value——引号中的字符串。

<html>
    <head>
        <style type="text/css">
            a[href="http://wwww.baidu.com"] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2> a[href="http://wwww.baidu.com"]</h2>
        <p><a name="wwww.baidu.com">wwww.baidu.com</p>
        <p><a href="http://wwww.baidu.com">http://wwww.baidu.com</p>
    </body>
</html>

在这里插入图片描述


3.[attr~=value] 匹配带有一个名为attr的属性的元素,其值正为value,或者匹配带有一个attr属性的元素,其值有一个或者更多,至少有一个和value匹配。注意,在一列中的好几个值,是用空格隔开的。

<html>
    <head>
        <style type="text/css">
            [class~="first"] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2> [class~="first"]</h2>
        <p><a class="first">first</p>
        <p><a class="first second">first second</p>
        <p><a class="second first ">second first </p>
    </body>
</html>

在这里插入图片描述


**4.[attr |=value] 匹配带有一个名为attr的属性的元素,其值可正为value,或者开始为value,后面紧随着一个连字符。连接符- **

<html>
    <head>
        <style type="text/css">
            [class|="first"] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2> [class|="first"]</h2>
        <p><a class="first">first</p>
        <p><a class="first second">first second</p>
        <p><a class="first-second">first-second</p>
        <p><a class="second first ">second first </p>
    </body>
</html>

在这里插入图片描述

子字符串匹配选择器

选择器 示例 描述
[attr^=value] li[class^=“box-”] 匹配带有一个名为attr的属性的元素,其值开头为value子字符串。
[attr$=value] li[class$=“-box”] 匹配带有一个名为attr的属性的元素,其值结尾为value子字符串
[attr*=value] li[class*=“box”] 匹配带有一个名为attr的属性的元素,其值的字符串中的任何地方,至少出现了一次value子字符串。

1 .|[attr^=value] 匹配带有一个名为attr的属性的元素,其值开头为value子字符串。

<html>
    <head>
        <style type="text/css">
            [class^="first"] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2> [class^="first"]</h2>
        <p><a class="first">first</p>
        <p><a class="first second">first second</p>
        <p><a class="first-second">first-second</p>
        <p><a class="second first ">second first </p>
    </body>
</html>

在这里插入图片描述


2.[attr$=value] 匹配带有一个名为attr的属性的元素,其值结尾为value子字符串

<html>
    <head>
        <style type="text/css">
            [class$="first"] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2> [class$="first"]</h2>
        <p><a class="first">first</p>
        <p><a class="first second">first second</p>
        <p><a class="first-second">first-second</p>
        <p><a class="second first t">second first t</p>
        <p><a class="second first">second first</p>
    </body>
</html>

在这里插入图片描述


3. [attr=value] 匹配带有一个名为attr的属性的元素,其值的字符串中的任何地方,至少出现了一次value子字符串。*

<html>
    <head>
        <style type="text/css">
            [class*="first"] {background-color: #009966;}
        </style>
    </head>
    <body>
        <h2> [class*="first"]</h2>
        <p><a class="first">first</p>
        <p><a class="first second">first second</p>
        <p><a class="second">second</p>
        <p><a class="second first first">second first first</p>
        <p><a class="second first">second first</p>
    </body>
</html>

在这里插入图片描述

相关文章

HTML代码中要想改变字体颜色,常常需要使用CSS样式表。CSS是...
HTML代码如何让字体盖住图片呢?需要使用CSS的position属性及...
HTML代码字体设置 在HTML中,我们可以使用标签来设置网页中的...
在网页设计中,HTML代码的字体和字号选择是非常重要的一个环...
HTML(Hypertext Markup Language,超文本标记语言)是一种用...
外链是指在一个网页中添加一个指向其他网站的链接,用户可以...