Definition and Usage
The [attribute~="value"] selector selects all elements that have a specified attribute whose value contains the word value.
For example, [class~="alert"] selects all elements with a class attribute value containing the word alert, such as class="alert caution" and class="warning alert". However, it does not select elements where the word is part of another word, such as class="warning-alert".
The [attribute~="value"] selector matches elements that have an attribute whose value is a space-separated list of words.
One of those words must be exactly value. If value contains spaces, the selector matches nothing (since words are separated by spaces).
It also does not match any elements if the value of the attribute is an empty string ("").
The following basic example makes this easier to understand.
Basic Example
/* Selects all elements whose class attribute value
contains the word "alert". */
[class~="alert"] {
background-color: darkblue;
color: white;
}
<p class="alert caution">alert caution</p>
<p class="warning alert">warning alert</p>
<p class="alert-caution">alert-caution</p> <!-- Not a space-separated word -->
<p class="alertcaution">alertcaution</p> <!-- Not a space-separated word -->
<p class="warning-alert">warning-alert</p> <!-- Not a space-separated word -->
<p class="warningalert">warningalert</p> <!-- Not a space-separated word -->
<p class="alert">alert</p> <!-- Selects when the entire value matches -->
alert caution
warning alert
alert-caution
alertcaution
warning-alert
warningalert
alert
Syntax
The following two syntaxes are equivalent.
[attribute~="value"][attribute~='value']
[class~="alert"] {
background-color: darkblue;
color: white;
}
/* The following is also equivalent. */
[class~='alert'] {
background-color: darkblue;
color: white;
}
Notes
The [attribute~="value"] selector matches elements based on word-level matching, where the value is treated as a space-separated list of words.
In contrast, the [attribute*="value"] selector matches elements based on substring matching, where the value is treated as a continuous string.
/* Selects all elements whose class attribute value
contains the string "alert". */
[class*="alert"] {
background-color: darkblue;
color: white;
}
<p class="alert caution">alert caution</p>
<p class="warning alert">warning alert</p>
<p class="alert-caution">alert-caution</p>
<p class="alertcaution">alertcaution</p>
<p class="warning-alert">warning-alert</p>
alert caution
warning alert
alert-caution
alertcaution
warning-alert
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
[attribute~="value"]
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
[attribute~="value"]
|
Selectors Level 4 #attribute-selectors |
References
See Also
- CSS [attribute] Selector – Select Elements by Attribute Name
- CSS [attribute="value"] Selector – Select Elements by Attribute Name and Value
- CSS [attribute*="value"] Selector – Select Elements Whose Attribute Value Contains a Substring
- CSS [attribute^="value"] Selector – Select Elements Whose Attribute Value Starts With a Specific Value
- CSS [attribute$="value"] Selector – Select Elements Whose Attribute Value Ends With a Specific Value
- CSS * Selector – Selects All Elements
- CSS Element Selector – Select by Element Name
- CSS Class Selector (.class) – Select Elements by Class Name
- HTML Attribute Reference