The [attribute] selector targets elements that have a specific HTML attribute, no matter what value it holds. It is a clean way to style links, form fields, and components without extra classes.
01
Presence
Attribute exists.
02
Syntax
[href]
03
Links
Style real URLs.
04
Forms
placeholder, type.
05
Combine
input[attr]
06
Family
More [attr] types.
Fundamentals
Introduction
The CSS [attribute] selector allows you to target elements based on the presence of a specific attribute, regardless of its value.
This is especially useful when you want uniform styles for every element that includes a particular attribute — such as all links with href, or all inputs with placeholder.
Definition and Usage
Wrap an attribute name in square brackets. The selector matches when that attribute exists on the element, even if the value is empty.
You can combine it with element names, classes, and pseudo-classes for more precise rules, such as input[placeholder] or a.external[href].
💡
Beginner Tip
[attribute] checks only whether the attribute is present. To match a specific value, use [attribute="value"] or other attribute operators covered in related tutorials.
Foundation
📝 Syntax
The signature of the presence attribute selector is:
syntax.css
[attribute]{/* CSS properties */}
Basic Example
href.css
[href]{color:#2563eb;text-decoration:underline;}
Syntax Rules
Matches any element that has the attribute, regardless of value.
Attribute names are case-insensitive in HTML for matching purposes.
Combine with element types: button[type], input[required].
Does not match elements that lack the attribute entirely.
Part of a larger attribute selector family (exact, contains, starts-with, etc.).
Attribute Selector Family
Selector
Meaning
[attribute]
Attribute is present (this tutorial)
[attribute="value"]
Exact value match
[attribute~="value"]
Word in a space-separated list
[attribute^="value"]
Value starts with string
[attribute$="value"]
Value ends with string
[attribute*="value"]
Value contains substring
Cheat Sheet
⚡ Quick Reference
Question
Answer
Selector type
Attribute presence
Matches when
Attribute exists on the element
Value required?
No — any value (or empty) matches
Common examples
[href], [placeholder], [disabled]
More specific
input[placeholder]
Browser support
All modern browsers
[href][placeholder]button[type][data-tooltip]
Context
When to Use [attribute]
The presence attribute selector is ideal when the attribute itself signals behavior or state:
Real links — Style <a href> differently from placeholder anchors without href.
Form hints — Highlight inputs that provide a placeholder.
Combining the element name with [placeholder] limits the rule to inputs only, even though the textarea also has a placeholder.
Watch Out
⚠️ Common Pitfalls
Inconsistent attributes — If only some elements include the attribute, others will not receive the style.
Empty vs missing — href="" still matches [href] because the attribute is present.
Over-broad rules — Bare [placeholder] affects every element type with that attribute; combine with element names when needed.
Boolean attributes — disabled and required can appear without a value; [disabled] still matches.
A11y
♿ Accessibility
Do not rely on styling alone — Use semantic HTML and ARIA when attributes convey state.
Placeholder is not a label — Always pair inputs with visible <label> elements.
Disabled state — Style [disabled] for visuals, but keep the attribute in HTML for assistive tech.
Link distinction — Styling [href] helps users spot real links vs inactive anchors.
🧠 How [attribute] Works
1
Browser scans elements
Each element’s HTML attributes are checked against the selector.
DOM
2
Presence is verified
[attribute] matches if the attribute exists, regardless of value.
Match
3
Styles are applied
Matched elements receive the CSS declarations from the rule.
Render
=
✅
Attribute-driven styling
Clean CSS that follows your HTML structure.
Compatibility
Browser Compatibility
CSS attribute selectors are supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera.
✓ Universal · All browsers
Attribute selectors everywhere
[attribute] has excellent cross-browser support on desktop and mobile.
99%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
[attribute] selector99% supported
Bottom line: Attribute presence selectors are safe for production use in modern projects.
Wrap Up
Conclusion
The CSS [attribute] selector is a flexible tool for styling elements based on the attributes they possess. It reduces reliance on extra classes and keeps your styles aligned with meaningful HTML.
Start with simple presence checks like [href] and [placeholder], then explore exact and partial value matchers as your projects grow more complex.