
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS [attribute=value] Selector
![CSS [attribute=value] Selector](https://codetofun.com/wp-content/themes/codetofun/images/large/css-attribute-equal-value-selector.webp)
Photo Credit to CodeToFun
Introduction
The CSS [attribute=value]
selector is used to select elements with a specific attribute value.
This allows for more precise styling by targeting elements based on their attribute values, making it a highly flexible tool for customizing the appearance of specific elements in a web page.
Syntax
The signature of the [attribute=value]
Selector is as follows:
[element[attribute="value"]] {
/* CSS properties */
}
- element: The HTML tag you want to target.
- attribute: The attribute you are checking for.
- value: The specific value of the attribute.
This selector only selects elements where the attribute has an exact match with the specified value.
Example
Here is an example of how to use the [attribute=value]
selector in CSS:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS [attribute=value] Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<a href="https://example.com" target="_blank">Visit Example</a>
<a href="https://example.com" target="_self">Open Example Here</a>
<button type="submit">Submit</button>
<button type="button">Click Me</button>
</body>
</html>
CSS
/* Style for links that open in a new tab */
a[target="_blank"] {
color: red;
text-decoration: underline;
}
/* Style for buttons of type "submit" */
button[type="submit"] {
background-color: green;
color: white;
border: 1px solid green;
}
/* Style for buttons of type "button" */
button[type="button"] {
background-color: blue;
color: white;
border: 1px solid blue;
}
In this example:
- Links (
<a>
) with thetarget="_blank"
attribute are styled with red text and underlined. - Buttons with the
type="submit"
attribute have a green background and white text. - Buttons with the
type="button"
attribute have a blue background and white text.
Usage Tips
- Use the
[attribute=value]
selector to precisely style elements based on attributes likehref
,target
,type
,class
, andid
. - Combine the
[attribute=value]
selector with other selectors like classes and pseudo-classes to fine-tune your styling.For example:
CSSCopiedinput[type="text"]:focus { border-color: blue; }
Common Pitfalls
- The
[attribute=value]
selector only matches exact values. For instance,[type="button"]
won't match an element if the type is set to something likebutton-primary
. - If you want to select elements with a partial match (e.g., values that start or contain a certain string), use the other attribute selectors like
[attribute^="value"]
(starts with) or[attribute*="value"]
(contains).
Conclusion
The CSS [attribute=value]
selector offers powerful control over styling by targeting elements based on their attributes. This selector is incredibly useful when working with form elements, links, and other attributes-driven HTML elements.
By mastering this selector, you can create more precise, maintainable styles for complex web pages.
Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS [attribute=value] Selector), please comment here. I will help you immediately.