Definition and Usage
The [attribute*="value"] selector selects all elements that have a specified attribute whose value contains the string value.
For example, [class*="alert"] selects all elements with class attribute values containing the string alert, such as class="alert caution" and class="warning-alert".
The [attribute*="value"] selector matches elements that have an attribute whose value contains the substring value.
If the value of the attribute is an empty string (""), the selector does not match any elements.
Basic Example
/* 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>
<p class="warningalert">warningalert</p>
<p class="alert">alert</p>
<p class="awesome">awesome</p>
alert caution
warning alert
alert-caution
alertcaution
warning-alert
warningalert
alert
awesome
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;
}
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 Word
- 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
- HTML Attribute Reference