CSS 选择器(属性选择器)

9 篇文章 0 订阅

CSS 选择器(属性选择器)

属性选择器

根据属性来为元素设置样式也是常用的场景。

选择器

示例

描述

[attribute]

[target]

带有 target 属性所有元素

[attribute=value]

[target=_blank]

targe 属性 等于"_blank" 的所有元素

[attribute~=value]

[title~=thief]

title 属性包含单词 "thief" 的所有元素

[attribute|=value]

[title|=hd]

title 属性值为 "hd"的单词,或hd-cms 以-连接的的独立单词

[attribute*=value]

a[src*="csdn"]

src 属性中包含 "csdn" 字符的每个 元素

[attribute^=value]

a[src^="https"]

src 属性值以 "https" 开头的每个 元素

[attribute$=value]

a[src$=".jpeg"]

src 属性以 ".jpeg" 结尾的所有 元素

为具有 class 属性的h1标签设置样式

h1[class] {

    colorred;

}

...

<h1 class="container">https://blog.csdn.net/weixin_44541438</h1>

 

约束多个属性

h1[class][id] {

    colorred;

}

...

 

<h1 class="container" id >https://blog.csdn.net/weixin_44541438</h1>

 

具体属性值设置样式

a[href="https://www.https://blog.csdn.net/weixin_44541438"] {

    colorgreen;

}

...

<a href="https://www.https://blog.csdn.net/weixin_44541438">后盾人</a>

<a href="">csdn</a>

 

^ 以指定值开头的元素

h2[name^="csdn"] {

    colorred;

}

...

 

<h2 name="Thief">https://blog.csdn.net/weixin_44541438</h2>

<h2 name="csdn.com">csdn.com</h2>

 

$ 以指定值结尾的元素

<h2 name="thief">https://blog.csdn.net/weixin_44541438</h2>

<h2 name="csdn.com">csdn.com</h2>

...

 

h2[name$="com"] {

    colorred;

}

 

* 属性内部任何位置出现值的元素

h2[name*="thief"] {

    colorred;

}

...

 

<h2 name="thief">https://blog.csdn.net/weixin_44541438</h2>

<h2 name="https://blog.csdn.net/weixin_44541438">csdn.com</h2>

 

~ 属性值中包含指定词汇的元素

h2[name~="thief"] {

    colorred;

}

...

 

<h2 name="thief">https://blog.csdn.net/weixin_44541438</h2>

<h2 name="thief web">csdn.com</h2>

 

| 以指定值开头或以属性连接破折号的元素

h2[name|="thief"] {

    colorred;

}

...

 

<h2 name="thief">https://blog.csdn.net/weixin_44541438</h2>

<h2 name="thief-web">csdn.com</h2>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值