CSS attribute selectors allow you to style HTML elements based on their attributes and attribute values. These selectors are particularly useful when you want to apply styles to elements that have specific attributes, regardless of their position in the document or their classes and IDs.
Basic Syntax
The basic syntax of an attribute selector is
element[attribute] { /* CSS properties */ }
This selector targets all elements of the specified type that have the given attribute, regardless of the attribute's value.
Types of Attribute Selectors
There are several types of attribute selectors you can use depending on your needs:
- [attribute]: Selects elements with the specified attribute, regardless of its value.
- [attribute="value"]: Selects elements with an attribute that matches exactly the given value.
- [attribute~="value"]: Selects elements with an attribute whose value is a space-separated list, one of which matches exactly the given value.
- [attribute|="value"]: Selects elements with an attribute whose value is a hyphen-separated list, starting with the given value.
- [attribute^="value"]: Selects elements with an attribute whose value starts with the given value.
- [attribute$="value"]: Selects elements with an attribute whose value ends with the given value.
- *[attribute="value"]**: Selects elements with an attribute whose value contains the given value as a substring.
Examples
Selecting Elements with a Specific Attribute
This example selects all<a>
(anchor) elements that have thetarget
attribute, regardless of the attribute's value.<style> a[target] { color: blue; } </style> <a href="https://www.djangoproject.in" target="_blank">Open in New Tab</a> <a href="https://www.djangoproject.in">Regular Link</a>
Only the first link will be blue because it has atarget
attribute.Selecting Elements with a Specific Attribute Value
This example selects all<input>
elements of typetext
<style> input[type="text"] { border: 1px solid #000; } </style> <input type="text" name="username"> <input type="password" name="password">
Only the text input will have a border.Selecting Elements with a Space-Separated Attribute Value
This example selects all elements with aclass
attribute that includes the word "primary".<style> [class~="primary"] { background-color: yellow; } </style> <div class="btn primary large">Button</div> <div class="btn secondary">Button</div>
The first<div>
will have a yellow background because it includes "primary" in its class list.Selecting Elements with a Hyphen-Separated Attribute Value
This example selects elements with alang
attribute that starts with "en".<style> [lang|="en"] { font-style: italic; } </style> <p lang="en-us">This is American English.</p> <p lang="en-gb">This is British English.</p> <p lang="fr">This is French.</p>
The first two paragraphs will be italicized.Selecting Elements with an Attribute Value Starting with a Specific String
This example selects<a>
elements with anhref
attribute that starts with "https".<style> a[href^="https"] { text-decoration: underline; } </style> <a href="https://www.djangoproject.in">Secure Link</a> <a href="http://www.djangoproject.in">Non-Secure Link</a>
Only the first link will be underlined.Selecting Elements with an Attribute Value Ending with a Specific String
This example selects<img>
elements with asrc
attribute that ends in ".jpg".<style> img[src$=".jpg"] { border-radius: 10px; } </style> <img src="image1.jpg"> <img src="image2.png">
Only the first image will have rounded corners.Selecting Elements with an Attribute Value Containing a Specific Substring
This example selects<a>
elements with anhref
attribute that contains the word "docs".<style> a[href*="css"] { font-weight: bold; } </style> <a href="https://www.djangoproject.in/css/what-is-css/">Documentation</a> <a href="https://www.djangoproject.in/about-us/">About Us</a>
The link to the documentation will be bold.
Practical Use Cases
Form Styling: Easily target specific form elements by their type, name, or other attributes.
Responsive Design: Apply styles based on data attributes or other custom attributes.
Custom Components: Style elements based on unique identifiers or status attributes.