It is possible to style HTML elements that have specific attributes or attribute values.
The [attribute]
selector is used to select elements with a specified attribute.
The following example selects all <a> elements with a target attribute:
The [attribute="value"]
selector is used to select elements with a specified attribute and value.
The following example selects all <a> elements with a target="_blank" attribute:
The [attribute~="value"]
selector is used to select elements with an attribute value containing a specified word.
The following example selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower":
The example above will match elements with title="flower", title="summer flower", and title="flower new", but not title="my-flower" or title="flowers".
The [attribute|="value"]
selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-).
Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".
The [attribute^="value"]
selector is used to select elements with the specified attribute, whose value starts with the specified value.
The following example selects all elements with a class attribute value that starts with "top":
Note: The value does not have to be a whole word!
The [attribute$="value"]
selector is used to select elements whose attribute value ends with a specified value.
The following example selects all elements with a class attribute value that ends with "test":
Note: The value does not have to be a whole word!
The [attribute*="value"]
selector is used to select elements whose attribute value contains a specified value.
The following example selects all elements with a class attribute value that contains "te":
Note: The value does not have to be a whole word!
The attribute selectors can be useful for styling forms without class or ID:
input[type="text"] {
width: 150px;
display: block;
margin-bottom: 10px;
background-color: yellow;
}
input[type="button"] {
width: 120px;
margin-left: 35px;
display: block;
}
Try it Yourself »
Tip: Visit our CSS Forms Tutorial for more examples on how to style forms with CSS.
Selector | Example | Example description |
---|---|---|
[attribute] | [target] | Selects all elements with a target attribute |
[attribute=value] | [target="_blank"] | Selects all elements with target="_blank" |
[attribute~=value] | [title~="flower"] | Selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower" |
[attribute|=value] | [lang|="en"] | Selects all elements with a lang attribute value starting with "en" |
[attribute^=value] | a[href^="https"] | Selects all <a> elements with a href attribute value starting with "https" |
[attribute$=value] | a[href$=".pdf"] | Selects all <a> elements with a href attribute value ending with ".pdf" |
[attribute*=value] | a[href*="91xjr"] | Selects all <a> elements with a href attribute value containing the substring "91xjr" |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!