定義と使い方
[attribute="value"]セレクタは、指定したattributeという名前の属性の値がvalueと一致するすべての要素を選択します。
例えば、[target="_blank"]は、文書内でtarget属性の値が_blankと一致するすべての要素を選択するセレクタです。
[attribute="value"]セレクタは、attribute属性の値が正確にvalueである要素を表します。
基本例
[target="_blank"] { /* target="_blank"を持つすべての要素を選択します。 */
background-color: darkblue;
color: white;
}
<a href="https://www.example.com" title="公式Webサイトへ移動">サンプルWebサイトへ移動</a>
<p>分かりやすく楽しい<abbr title="Hypertext Markup Language">HTML</abbr>を学びましょう!</p>
<a target="_blank" href="https://www.example.com">サンプルWebサイトを新しいタブで開く</a>
構文
次の2つの記述は、どちらも同じ意味です。
[attribute="value"][attribute='value']
[target="_blank"] {
background-color: darkblue;
color: white;
}
/* 次のように記述しても同じです。 */
[target='_blank'] {
background-color: darkblue;
color: white;
}
ブラウザーの互換性
| セレクター |
デスクトップ Chrome
|
デスクトップ Edge
|
デスクトップ Firefox
|
Safari
|
|---|---|---|---|---|
[attribute="value"]
|
1 | 12 | 1 | 3 |
仕様書
| 仕様書 | |
|---|---|
[attribute="value"]
|
Selectors Level 4 #attribute-selectors |