CSS Attribute Selectors
1. Introduction to CSS Attribute Selectors
CSS Attribute Selectors target elements based on their attributes instead of using classes or IDs. They are especially useful when styling links, form controls, images, and custom HTML attributes.
Why Use Attribute Selectors?
- Select elements without adding extra classes.
- Write cleaner and reusable CSS.
- Style elements based on attribute values.
- Improve code maintainability.
- Useful for forms, links, and responsive designs.
a[href]{
color:blue;
}
2. What are CSS Attribute Selectors?
An attribute selector selects HTML elements that have a specific attribute or a particular attribute value.
selector[attribute]{
property:value;
}
| Selector | Description |
|---|---|
| [attribute] | Selects elements containing the attribute. |
| [attribute="value"] | Selects elements with an exact value. |
| [attribute~="value"] | Selects elements containing a word. |
| [attribute|="value"] | Selects elements beginning with a value. |
| [attribute^="value"] | Selects values starting with a string. |
| [attribute$="value"] | Selects values ending with a string. |
| [attribute*="value"] | Selects values containing a string. |
3. [attribute] Selector
The [attribute] selector selects every element
that contains the specified attribute, regardless of its value.
a[href]{
color:red;
}
img[alt]{
border:2px solid green;
}
| Selector | Selects |
|---|---|
| a[href] | All links having an href attribute. |
| img[alt] | All images having an alt attribute. |
4. [attribute="value"] Selector
The [attribute="value"] selector selects
elements whose attribute exactly matches the specified value.
input[type="text"]{
border:2px solid blue;
}
a[target="_blank"]{
color:green;
}
| Selector | Matches |
|---|---|
| input[type="text"] | Only text input fields. |
| a[target="_blank"] | Links opening in a new tab. |
5. [attribute~="value"] Selector
The [attribute~="value"] selector selects
elements whose attribute value contains the specified word.
The words must be separated by spaces.
p[class~="intro"]{
color:purple;
}
div[class~="box"]{
border:2px solid red;
}
| Selector | Description |
|---|---|
| p[class~="intro"] | Selects paragraphs whose class contains the word "intro". |
| div[class~="box"] | Selects div elements containing the word "box". |
~= operator matches complete words only, not partial words.
6. [attribute|="value"] Selector
The [attribute|="value"] selector selects
elements whose attribute value is exactly the specified value
or begins with that value followed by a hyphen (-).
p[lang|="en"]{
color:blue;
}
Example HTML:
<p lang="en">English</p>
<p lang="en-US">American English</p>
<p lang="fr">French</p>
| Selector | Selects |
|---|---|
| p[lang|="en"] | Elements with lang="en" and lang="en-US". |
|= operator is commonly used for language attributes.
7. [attribute^="value"], [attribute$="value"] & [attribute*="value"]
These selectors match attribute values based on the beginning, ending, or occurrence of a specific string.
| Selector | Description |
|---|---|
| [attribute^="value"] | Selects values that start with the specified text. |
| [attribute$="value"] | Selects values that end with the specified text. |
| [attribute*="value"] | Selects values containing the specified text. |
a[href^="https"]{
color:green;
}
img[src$=".png"]{
border:2px solid blue;
}
a[href*="google"]{
font-weight:bold;
}
8. Practical CSS Attribute Selector Examples
Example 1 : Style External Links
a[target="_blank"]{
color:red;
}
Example 2 : Style Required Inputs
input[required]{
border:2px solid red;
}
Example 3 : Highlight PDF Links
a[href$=".pdf"]{
color:purple;
}
Example 4 : Style Secure Websites
a[href^="https"]{
color:green;
}
9. Advantages of CSS Attribute Selectors
- Select elements without extra classes or IDs.
- Create cleaner HTML code.
- Reduce duplicate CSS.
- Style elements dynamically.
- Useful for forms and hyperlinks.
- Easy to maintain large websites.
- Improve CSS readability.
| Feature | Available |
|---|---|
| Flexible Selection | ✔ Yes |
| Cleaner HTML | ✔ Yes |
| Easy Maintenance | ✔ Yes |
10. CSS Attribute Selector Comparison Table
| Selector | Purpose | Example |
|---|---|---|
| [attribute] | Attribute exists | a[href] |
| [attribute="value"] | Exact match | input[type="text"] |
| [attribute~="value"] | Contains a whole word | div[class~="box"] |
| [attribute|="value"] | Exact value or starts with value- | p[lang|="en"] |
| [attribute^="value"] | Starts with text | a[href^="https"] |
| [attribute$="value"] | Ends with text | img[src$=".png"] |
| [attribute*="value"] | Contains text | a[href*="google"] |
input[required]{
border:2px solid red;
}
a[href^="https"]{
color:green;
}
img[src$=".jpg"]{
border-radius:10px;
}
11. CSS Attribute Selector Best Practices
Following best practices helps you write cleaner, maintainable, and efficient CSS using attribute selectors.
- Use attribute selectors only when necessary.
- Avoid long and overly specific selectors.
- Use classes for repeated styling patterns.
- Use attribute selectors for forms and links.
- Keep selectors readable and organized.
- Combine attribute selectors with other selectors carefully.
- Test selectors after changing HTML attributes.
- Keep your CSS simple and maintainable.
12. Common CSS Attribute Selector Mistakes
| Mistake | Correct Practice |
|---|---|
| Using attribute selectors everywhere. | Use classes when styling many elements. |
| Using the wrong operator. | Select the correct operator (=, ~=, |=, ^=, $=, *=). |
| Creating overly complex selectors. | Keep selectors short and readable. |
| Ignoring browser testing. | Test your website in multiple browsers. |
| Using attribute selectors for important application logic. | Use JavaScript when behavior is required. |
❌ Poor
div[class*="box"][data-id][title]{
color:red;
}
✔ Better
.box{
color:red;
}
13. Browser Support
CSS Attribute Selectors are supported by all modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Attribute Selector Interview Questions
- What is a CSS Attribute Selector?
- Why are attribute selectors useful?
- What is the difference between
[attribute]and[attribute="value"]? - What does the
~=operator do? - What is the purpose of the
|=operator? - When would you use the
^=selector? - What does the
$=selector match? - How does the
*=selector work? - Can attribute selectors be combined with other selectors?
- Are CSS Attribute Selectors supported in modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Attribute Selectors.
- ✔ Different types of attribute selectors.
- ✔
[attribute]selector. - ✔
[attribute="value"]selector. - ✔
[attribute~="value"]selector. - ✔
[attribute|="value"]selector. - ✔
[attribute^="value"],[attribute$="value"], and[attribute*="value"]. - ✔ Practical examples.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
Quick Quiz
Which CSS selector selects all elements that contain an href attribute?