You can apply styles to HTML elements that possess particular attributes or attribute values.
The [attribute] selector targets elements with a designated attribute.
In the example below, it selects all <a> elements possessing a target attribute.
Example
a[target] { |
The [attribute=”value”] selector identifies elements with a specific attribute and value.
In the example provided, it selects all <a> elements with a target=”_blank” attribute.
Example
a[target=”_blank”] { background-color: yellow; } |
The [attribute~=”value”] selector targets elements with an attribute value containing a specified word.
In the following example, it selects all elements with a title attribute containing a list of space-separated words, one of which is “flower”.
Example
[title~=”flower”] { |
The provided example will match elements with titles such as “flower”, “summer flower”, and “flower new”, but will not match titles like “my-flower” or “flowers”.
The [attribute|=”value”] selector targets elements with the specified attribute, where the value can either exactly match the specified value or be followed by a hyphen (-).
Note: The value must be a complete word, either standalone like class=”top”, or followed by a hyphen (-) as in class=”top-text”.
Example
[class|=”top”] { background: yellow; } |
The [attribute^=”value”] selector targets elements with the specified attribute, where the value begins with the specified string.
In the given example, it selects all elements with a class attribute value that commences with “top”.
Note: The value does not necessarily have to be a complete word.
Example
[class^=”top”] { background: yellow; } |
The [attribute$=”value”] selector is employed to choose elements with an attribute value that concludes with a specified string.
In the provided example, it selects all elements with a class attribute value that concludes with “test”.
Note: The value does not need to be a complete word.
Example
[class$=”test”] { background: yellow; } |
The [attribute*=”value”] selector targets elements with an attribute value that includes a specified string.
In the given example, it selects all elements with a class attribute value that contains “te”.
Note: The value doesn’t necessarily have to be a complete word.
Example
[class*=”te”] { background: yellow; } |
Attribute selectors are beneficial for styling forms lacking specific class or ID attributes.
Example
input[type=”text”] { |