css - Attribute Selectors Part 1: Basic Attribute Selectors
The basic attribute selector targets elements that have a specified attribute, regardless of its value.
Syntax:
[attribute] {
/* styles */
}
Examples:
Targeting Links with an href Attribute
[a] {
color: blue;
text-decoration: underline;
}
<a href="example.com">Link with href</a>
<a>Link without href</a>
<!-- Only the first link gets styled -->
Styling Input Fields with a required Attribute
[input] {
border: 2px solid green;
}
<input type="text" required>
<input type="password">
<!-- Only the input with `required` gets styled -->
Highlighting Images with an alt Attribute
[alt] {
border: 3px dashed orange;
}
<img src="image.jpg" alt="Example Image">
<img src="image2.jpg">
<!-- Only the first image gets a border -->