CSS [attribute] Selector

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Attribute Selectors

What You’ll Learn

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.

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.

📝 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

SelectorMeaning
[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

⚡ Quick Reference

QuestionAnswer
Selector typeAttribute presence
Matches whenAttribute exists on the element
Value required?No — any value (or empty) matches
Common examples[href], [placeholder], [disabled]
More specificinput[placeholder]
Browser supportAll modern browsers
[href] [placeholder] button[type] [data-tooltip]

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.
  • Typed buttons — Style button[type] submit/reset controls.
  • Disabled controls — Target [disabled] without adding a class.
  • Custom data — Style elements with data-* attributes for JS hooks.

👀 Live Preview

Links with href and inputs with placeholder are styled automatically:

Examples Gallery

Practice [attribute] with links, form fields, buttons, and combined selectors.

📜 Core Patterns

Target elements that have a specific HTML attribute present.

Example 1 — Style links with href

Apply link styles only to anchors that actually have an href attribute.

href-links.css
[href] {
  color: #2563eb;
  text-decoration: underline;
}
Try It Yourself

How It Works

The first anchor has href and receives link styling. The second anchor lacks href, so [href] does not match it.

Example 2 — Inputs with placeholder

Give inputs that include hint text a distinct green border.

placeholder.css
[placeholder] {
  border: 2px solid #16a34a;
  padding: 5px;
  border-radius: 6px;
}
Try It Yourself

How It Works

Only the input with a placeholder attribute gets the green border. The plain input is unchanged.

📄 Combined Selectors

Narrow attribute rules to specific element types.

Example 3 — Buttons with a type attribute

Style submit and other typed buttons using button[type].

button-type.css
button[type] {
  background-color: #facc15;
  border: none;
  padding: 10px;
  cursor: pointer;
  border-radius: 6px;
}
Try It Yourself

How It Works

button[type] matches any button that declares a type attribute, regardless of whether it is submit, button, or reset.

Example 4 — Element + attribute combined

Use input[placeholder] so only inputs — not textareas or other elements — get the rule.

input-placeholder.css
input[placeholder] {
  border: 2px solid #2563eb;
  padding: 0.5rem;
  border-radius: 6px;
}
Try It Yourself

How It Works

Combining the element name with [placeholder] limits the rule to inputs only, even though the textarea also has a placeholder.

⚠️ Common Pitfalls

  • Inconsistent attributes — If only some elements include the attribute, others will not receive the style.
  • Empty vs missinghref="" 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 attributesdisabled and required can appear without a value; [disabled] still matches.

♿ 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
[attribute] selector 99% supported

Bottom line: Attribute presence selectors are safe for production use in modern projects.

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.

💡 Best Practices

✅ Do

  • Use [href] to distinguish real links
  • Combine element + attribute for precision
  • Leverage boolean attributes like [disabled]
  • Use data-* attributes for styling hooks
  • Learn the full attribute selector family

❌ Don’t

  • Assume missing attributes will still match
  • Replace labels with placeholders alone
  • Write overly broad [attribute] rules
  • Forget that empty attributes still count as present
  • Mix up presence [attr] with exact [attr="x"]

Key Takeaways

Knowledge Unlocked

Five things to remember about [attribute]

Use these points when targeting HTML attributes.

5
Core concepts
🔗 02

[href]

Real links.

Pattern
📝 03

Forms

placeholder, type.

Uses
🔨 04

Combine

input[attr].

Precision
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The [attribute] selector matches any element that has the specified HTML attribute, regardless of the attribute's value.
[href] matches any element with an href attribute. [href="https://example.com"] matches only when the value is an exact match.
Yes. input[placeholder] selects only input elements that have a placeholder attribute.
In HTML documents, attribute names are generally treated as case-insensitive, but attribute values in selectors should match the HTML exactly.
Yes. Attribute selectors are supported in all modern browsers and have been part of CSS for many years.

Practice in the Live Editor

Open the HTML editor and experiment with [href], [placeholder], and combined attribute selectors.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful