CSS3属性选择器学习

Css3的属性选择器在各种情况下的生效情况

1.element[attribute], 选择所有带有attribute属性元素,例:

a[href] {
 color: red;  /*将带有href属性的a元素文本设为红色*/
}

2.element[attribute=“value”],选择attribute属性值等于"value"的元素,例:

a[href="#"] {
 color: #f00;
}

要注意的是,当属性值可以有多个值时,其中的一个值等于value,是不生效的。例:

<style type="text/css">
 a[class="one"] {
  color: #f00;
 }
</style>
<body>
 <a class="one two">attribute</a>
</body>

在这里插入图片描述

3.element[attribute~=“value”],选择attribute属性包含单词“value”的元素

<style type="text/css">
 a[href~="#"] {
  color: #f00;
 }
 a[class~="one"] {
  color: #0f0;
 }
</style>
</head>
<body>
 <a href="#">attribute</a>
 <a href="#1">attribute</a>
 <a class="one">attribute</a>
 <a class="one two">attribute</a>
</body>

在这里插入图片描述
即:选择属性值列表(有多个属性值)中有一个值等于value的元素。当值的子串中包含value时,还是失效的,如第二个a元素href的值为“#1”,虽然“#1”的子串中有“#”。

4.element[attribute^=“value”],选择attribute属性值以“value”开头的所有元素

<style type="text/css">
 a[href^="#"] {
  color: red;
 }
 a[class^="one"] {
  color: aqua;
 }
</style>
</head>
<body>
 <a href="#">attribute</a>
 <a href="#1">attribute</a>
 <a href="1#">attribute</a>
 <a href="1#1">attribute</a>
 <a href="#-1">attribute</a>
 <a class="oneTwo">attribute</a>
 <a class="one two">attribute</a>
 <a class="two one">attribute</a>
</body>

在这里插入图片描述即:选择属性值列表以value开头的元素,且value可为属性值的子串。如第2,5,6个a元素href的值为"#1",“#-1”,“oneTwo”。而第7,8个a元素有多个class值,但第八个的属性值列表并不是以“one”开头,所以只有是以“one”开头的第7个a元素生效。

5.element[attribute$=“value”],选择attribute属性值以“value”结尾的所有元素

<style type="text/css">
 a[href$="#"] {
 color: red;
}
 a[class$="two"] {
  color: aqua;
 }
</style>
</head>
<body>
 <a href="#">attribute</a>
 <a href="1#">attribute</a>
 <a class="onetwo">attribute</a>
 <a class="one two">attribute</a>
 <a class="one onetwo">attribute</a>
</body>

在这里插入图片描述即:选择属性值列表中以value结尾的元素,value可为属性值的子串。

6.element[attribute*=“value”],选择attribute属性值包含"value"的所有元素

<style type="text/css">
 a[href*="#"] {
  color: red;
 }
 a[class*="two"] {
  color: aqua;
 }
</style>
</head>
<body>
 <a href="#">attribute</a>
 <a href="1#">attribute</a>
 <a class="one two">attribute</a>
 <a class="one onetwo">attribute</a>
</body>

在这里插入图片描述即:只要属性值列表的子串中包含value,就可以生效。

7.element[attribute|=“value”],选择attribute属性值为“value”或以“value-”开头的元素

<style type="text/css">
 a[href|="#"] {
  color: red;
 }
 a[class|="one"] {
  color: aqua;
 }
</style>
</head>
<body>
 <a href="#">attribute</a>
 <a href="#1">attribute</a>
 <a href="#-1">attribute</a>
 <a class="one">attribute</a>
 <a class="one two">attribute</a>
 <a class="one-two three">attribute</a>
 <a class="three one-two">attribute</a>
</body>

在这里插入图片描述
只有第1,3,4,6个元素生效了,即选择属性值列表以value或value-开头的元素。

新人第一帖,有错误或疏漏请大佬们指正或补充!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值