The CSS attribute selector matches elements based on the presence or value of a given attribute.
/* <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;
}
/* <a> elements whose class attribute contains the word "logo" */
a[class~="logo"] {
padding: 2px;
}
[
attr
]
[
attr
=
value
]
[
attr
~=
value
]
[
attr
|=
value
]
-
(U+002D). It is often used for language subcode matches.
[
attr
^=
value
]
[
attr
$=
value
]
[
attr
*=
value
]
[
attr
operator
value
i]
i
(或
I
) before the closing bracket causes the value to be compared case-insensitively (for characters within the ASCII range).
[
attr
operator
value
s]
s
(或
S
) before the closing bracket causes the value to be compared case-sensitively (for characters within the ASCII range).
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;
}
<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>
/* 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-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>
The HTML specification requires the
type
attribute to be matched case-insensitively due to it primarily being used in the
<input>
element, trying to use attribute selectors to with the
type
attribute of an
ordered list
doesn't work without the
case-sensitive
modifier.
/* List types require the case sensitive flag due to a quirk in how HTML treats the type attribute. */
ol[type="a"] {
list-style-type: lower-alpha;
background: red;
}
ol[type="a" s] {
list-style-type: lower-alpha;
background: lime;
}
ol[type="A" s] {
list-style-type: upper-alpha;
background: lime;
}
<ol type="A"> <li>Example list</li> </ol>
| 规范 | 状态 | 注释 |
|---|---|---|
|
Selectors Level 4
The definition of 'attribute selectors' in that specification. |
工作草案 | Adds modifier for ASCII case-sensitive and case-insensitive attribute value selection. |
|
Selectors Level 3
The definition of 'attribute selectors' in that specification. |
推荐 | |
|
CSS Level 2 (Revision 1)
The definition of 'attribute selectors' in that specification. |
推荐 | 初始定义 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Attribute selector (
[attr=value]
)
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 7 | Opera 完整支持 9 | Safari 完整支持 3 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
Case-insensitive modifier (
i
)
|
Chrome 完整支持 49 | Edge 完整支持 79 | Firefox 完整支持 47 | IE 不支持 No | Opera 完整支持 36 | Safari 完整支持 9 | WebView Android 完整支持 49 | Chrome Android 完整支持 49 | Firefox Android 完整支持 47 | Opera Android 完整支持 36 | Safari iOS 完整支持 9 | Samsung Internet Android 完整支持 5.0 |
Case-sensitive modifier (
s
)
|
Chrome
不支持
No
注意事项
|
Edge
不支持
No
注意事项
|
Firefox 完整支持 66 | IE 不支持 No |
Opera
不支持
No
注意事项
|
Safari 不支持 No |
WebView Android
不支持
No
注意事项
|
Chrome Android
不支持
No
注意事项
|
Firefox Android 完整支持 66 |
Opera Android
不支持
No
注意事项
|
Safari iOS 不支持 No |
Samsung Internet Android
不支持
No
注意事项
|
完整支持
不支持
见实现注意事项。
attr()
Document.querySelector()
,
DocumentFragment.querySelector()
,或
Element.querySelector()
Document.querySelectorAll()
,
DocumentFragment.querySelectorAll()
,或
Element.querySelectorAll()
ParentNode
mixin; see
ParentNode.querySelector()
and
ParentNode.querySelectorAll()
Type selectors
Class selectors
ID selectors
Universal selectors
Attribute selectors
:active
:any-link
:checked
:blank
:default
:defined
:dir()
:disabled
:empty
:enabled
:first
:first-child
:first-of-type
:fullscreen
:focus
:focus-visible
:focus-within
:has()
:host()
:host-context()
:hover
:indeterminate
:in-range
:invalid
:is() (:matches(), :any())
:lang()
:last-child
:last-of-type
:left
:link
:not()
:nth-child()
:nth-last-child()
:nth-last-of-type()
:nth-of-type()
:only-child
:only-of-type
:optional
:out-of-range
:placeholder-shown
:read-only
:read-write
:required
:right
:root
:scope
:target
:valid
:visited
:where()
::-moz-progress-bar
::-moz-range-progress
::-moz-range-thumb
::-moz-range-track
::-webkit-progress-bar
::-webkit-progress-value
::-webkit-slider-runnable-track
::-webkit-slider-thumb
::after (:after)
::backdrop
::before (:before)
::cue
::cue-region
::first-letter (:first-letter)
::first-line (:first-line)
::grammar-error
::marker
::part()
::placeholder
::selection
::slotted()
::spelling-error