属性选择器
Baseline 广泛可用 *
CSS 属性选择器根据元素是否明确设置了给定属性来匹配元素,并提供定义属性值或子字符串值匹配的选项。
语法
/* <a> elements with a title attribute */
a[title] {
color: purple;
}
/* <a> elements with an href matching "https://example.org" */
a[href="https://example.org"]
{
color: green;
}
/* <a> elements with an href containing "example" */
a[href*="example"] {
font-size: 2em;
}
/* <a> elements with an href ending ".org", case-insensitive */
a[href$=".org" i] {
font-style: italic;
}
/* <a> elements whose class attribute contains the word "logo" */
a[class~="logo"] {
padding: 2px;
}
[attr]
-
表示具有属性名称为 attr 的元素。
[attr=value]
-
表示具有属性名称为 attr 且其值恰好为 value 的元素。
[attr~=value]
-
表示具有属性名称为 attr 且其值是空格分隔的单词列表,其中之一恰好为 value 的元素。
[attr|=value]
-
表示具有属性名称为 attr 且其值恰好为 value 或以 value 开头并紧跟连字符
-
(U+002D) 的元素。它通常用于语言子代码匹配。 [attr^=value]
-
表示具有属性名称为 attr 且其值以 value 为前缀(开头)的元素。
[attr$=value]
-
表示具有属性名称为 attr 且其值以 value 为后缀(结尾)的元素。
[attr*=value]
-
表示具有属性名称为 attr 且其值包含字符串中至少一个 value 的元素。
[attr operator value i]
-
在闭合括号前添加
i
(或I
)会使值进行不区分大小写的比较(对于 ASCII 范围内的字符)。 [attr operator value s]
-
在闭合括号前添加
s
(或S
)会使值进行区分大小写的比较(对于 ASCII 范围内的字符)。
值
描述
属性名称和值的区分大小写取决于文档语言。在 HTML 中,属性名称不区分大小写,规范定义的枚举值也是如此。不区分大小写的 HTML 属性值列在 HTML 规范中。对于这些属性,选择器中的属性值不区分大小写,无论该值是否无效或设置该属性的元素上的属性是否无效。
如果属性值区分大小写,例如 class
、id
和 data-*
属性,则属性选择器值匹配区分大小写。HTML 规范之外定义的属性,如 role
和 aria-*
属性,也区分大小写。区分大小写的属性选择器可以通过包含不区分大小写修饰符(i
)来使其不区分大小写。
示例
链接
CSS
a {
color: blue;
}
/* Internal links, beginning with "#" */
a[href^="#"] {
background-color: gold;
}
/* Links with "example" anywhere in the URL */
a[href*="example"] {
background-color: silver;
}
/* Links with "insensitive" anywhere in the URL,
regardless of capitalization */
a[href*="insensitive" i] {
color: cyan;
}
/* Links with "cAsE" anywhere in the URL,
with matching capitalization */
a[href*="cAsE" s] {
color: pink;
}
/* Links that end in ".org" */
a[href$=".org"] {
color: red;
}
/* Links that start with "https://" and end in ".org" */
a[href^="https://"][href$=".org"]
{
color: green;
}
HTML
<ul>
<li><a href="#internal">Internal link</a></li>
<li><a href="http://example.com">Example link</a></li>
<li><a href="#InSensitive">Insensitive internal link</a></li>
<li><a href="http://example.org">Example org link</a></li>
<li><a href="https://example.org">Example https org link</a></li>
</ul>
结果
语言
CSS
/* All divs with a `lang` attribute are bold. */
div[lang] {
font-weight: bold;
}
/* All divs without a `lang` attribute are italicized. */
div:not([lang]) {
font-style: italic;
}
/* All divs in US English are blue. */
div[lang~="en-us"] {
color: blue;
}
/* All divs in Portuguese are green. */
div[lang="pt"] {
color: green;
}
/* All divs in Chinese are red, whether
simplified (zh-Hans-CN) or traditional (zh-Hant-TW). */
div[lang|="zh"] {
color: red;
}
/* All divs with a Traditional Chinese
`data-lang` are purple. */
/* Note: You could also use hyphenated attributes
without double quotes */
div[data-lang="zh-Hant-TW"] {
color: purple;
}
HTML
<div lang="en-us en-gb en-au en-nz">Hello World!</div>
<div lang="pt">Olá Mundo!</div>
<div lang="zh-Hans-CN">世界您好!</div>
<div lang="zh-Hant-TW">世界您好!</div>
<div data-lang="zh-Hant-TW">世界您好!</div>
结果
HTML 有序列表
HTML 规范要求 type
属性不区分大小写匹配,因为它主要用于 <input>
元素。请注意,如果用户代理不支持修饰符,则选择器将不匹配。
CSS
/* Case-sensitivity depends on document language */
ol[type="a"]:first-child {
list-style-type: lower-alpha;
background: red;
}
ol[type="i" s] {
list-style-type: lower-alpha;
background: lime;
}
ol[type="I" s] {
list-style-type: upper-alpha;
background: grey;
}
ol[type="a" i] {
list-style-type: upper-alpha;
background: green;
}
HTML
<ol type="A">
<li>
Red background for case-insensitive matching (default for the type selector)
</li>
</ol>
<ol type="i">
<li>Lime background if `s` modifier is supported (case-sensitive match)</li>
</ol>
<ol type="I">
<li>Grey background if `s` modifier is supported (case-sensitive match)</li>
</ol>
<ol type="A">
<li>
Green background if `i` modifier is supported (case-insensitive match)
</li>
</ol>
结果
规范
规范 |
---|
选择器 Level 4 # attribute-selectors |
浏览器兼容性
加载中…