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" */
a[href$=".org"] {
font-style: italic;
}
[attr]- 表示带有以 attr 命名的属性的元素。
[attr=value]- 表示带有以 attr 命名的,且值为"value"的属性的元素。
[attr~=value]- 表示带有以 attr 命名的属性的元素,并且该属性是一个以空格作为分隔的值列表,其中至少一个值为"value"。
[attr|=value]- 表示带有以 attr 命名的属性的元素,属性值为“value”或是以“value-”为前缀("-"为连字符,Unicode编码为U+002D)开头。典型的应用场景是用来来匹配语言简写代码(如zh-CN,zh-TW可以用zh作为value)。
[attr^=value]- 表示带有以 attr 命名的,且值是以"value"开头的属性的元素。
[attr$=value]- 表示带有以 attr 命名的,且值是以"value"结尾的属性的元素。
[attr*=value]- 表示带有以 attr 命名的,且值包含有"value"的属性的元素。
[attr operator value i]- 在带有属性值的属性选型选择器表达式的右括号(]括号)前添加用空格间隔开的字母i(或I)可以忽略属性值的大小写(ASCII字符范围内的字母)
示例
Links
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 that end in ".org" */
a[href$=".org"] {
color: red;
}
<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> </ul>
Languages
/* All divs with a `lang` attribute are bold. */
div[lang] {
font-weight: bold;
}
/* 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-CN) or traditional (zh-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-TW"] {
color: purple;
}
<div lang="en-us en-gb en-au en-nz">Hello World!</div> <div lang="pt">Olá Mundo!</div> <div lang="zh-CN">世界您好!</div> <div lang="zh-TW">世界您好!</div> <div data-lang="zh-TW">世界您好!</div>
规范
| Specification | Status | Comment |
|---|---|---|
| Selectors Level 4 attribute selectors |
Working Draft | Added modifier for ASCII case-insensitive attribute value selection. |
| Selectors Level 3 attribute selectors |
Recommendation | |
| CSS Level 2 (Revision 1) attribute selectors |
Recommendation | 初始定义 |
浏览器支持
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | 1.0 (1.7 or earlier) | 7 | 9 | 3 |
| Case-insensitive modifier | 49.0 | 47.0 (47.0) | ? | ? | 9 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | ? | (Yes) | 1.0 (1) | ? | ? | ? | (Yes) |
| Case-insensitive modifier | ? | 49.0 | 47.0 (47.0) | ? | ? | 9 | 49.0 |