定義と使い方
[attribute$="value"]セレクタは、
指定したattributeという名前の属性の値が、valueという値で終わるすべての要素を選択します。
例えば、[href$=".html"]は、href="home.html"やhref="contact.html"のように、href属性の値が.htmlで終わるすべての要素を選択するセレクタです。
[attribute$="value"]セレクタは、値が接尾辞valueで終わるattribute属性を持つ要素を表します。attribute属性の値が空文字列("")の場合、このセレクタは何も選択しません。
以下の基本的な例を見ると、簡単に理解できます。
基本例
/* href属性の値が
".html"で終わるすべての要素を選択します。 */
[href$=".html"] {
background-color: darkblue;
color: white;
}
[href$=".html"]::after {
content: " 📗";
}
<p>次のリンクをご覧ください。</p>
<ul>
<li><a href="home.html">ホーム</a></li>
<li><a href="contact.html">お問い合わせ</a></li>
<li><a href="https://example.com/">Example Domain</a></li>
</ul>
次のリンクをご覧ください。
構文
次の2つの記述は、どちらも同じ意味です。
[attribute$="value"][attribute$='value']
[href$=".html"] {
background-color: darkblue;
color: white;
}
/* 次のように記述しても同じです。 */
[href$='.html'] {
background-color: darkblue;
color: white;
}
参考事項
[attribute$="value"]は、属性の値が特定の値で終わる要素を選択します。
一方、[attribute^="value"]セレクタは、属性の値が特定の値で始まる要素を選択します。
/* href属性の値が
"#"で始まるすべての要素を選択します。 */
[href^="#"] {
background-color: darkblue;
color: white;
}
[href^="#"]::after {
content: " 👇";
}
<p>次のリンクをご覧ください。</p>
<ul>
<li><a href="#subject-1">トピック-1</a></li>
<li><a href="#contact">お問い合わせ</a></li>
<li><a href="https://example.com/">Example Domain</a></li>
</ul>
次のリンクをご覧ください。
ブラウザーの互換性
| セレクター |
デスクトップ Chrome
|
デスクトップ Edge
|
デスクトップ Firefox
|
Safari
|
|---|---|---|---|---|
[attribute$="value"]
|
1 | 12 | 1 | 3 |
仕様書
| 仕様書 | |
|---|---|
[attribute$="value"]
|
Selectors Level 4 #attribute-selectors |