CHAPTER 36
CSS Attribute Selectors
Learn how CSS can select HTML elements based on their attributes and attribute values.
01. What is CSS Attribute Selectors?
Attribute selectors select elements based on HTML attributes.
They are useful for styling specific inputs, links and custom elements.
CSS provides several attribute matching patterns.
02. Syntax
[attribute] {
property: value;
}
[attribute="value"] {
property: value;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
input[type="text"] {
border: 2px solid #2563eb;
padding: 10px;
}
input[type="email"] {
border: 2px solid #06b6d4;
padding: 10px;
}
a[target="_blank"] {
color: #ec4899;
}
</style>
</head>
<body>
<input
type="text"
placeholder="Name"
>
<br><br>
<input
type="email"
placeholder="Email"
>
<br><br>
<a href="#" target="_blank">
Open Link
</a>
</body>
</html>
04. Important Points
✓
[attribute] selects elements containing an attribute.
✓
[attribute=value] selects a specific attribute value.
✓
Attribute selectors are useful for form controls.
✓
They can make CSS more precise without adding extra classes.
05. Common Mistakes
⚠
Writing attribute values incorrectly.
⚠
Using overly broad attribute selectors.
⚠
Creating selectors that are unnecessarily difficult to maintain.
06. Real World Use
Attribute selectors are useful for forms, external links, input types and component styling.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Style Different Inputs
Create text, email and password inputs and give each input type a different border color using attribute selectors.