屬性選擇器
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 |
瀏覽器相容性
載入中…