CSS Attribute Selector ([attr])

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The attribute presence selector [attr] matches any element that has that attribute set in the HTML—the value does not matter.

Example: a[title] styles every link that has a title attribute. Other operators (=, *=, ^=, …) match by value; those have their own pages below.

Note Presence vs value

[href] matches any element with an href. [href="https://example.org"] matches only that exact value. See equals.

Limited Case flags i / s

Value operators can add i (ASCII case-insensitive) or s (case-sensitive). Support for s varies more than for base attribute selectors.

CSS Syntax

syntax.css
[attr] { … }

Optional element type: a[title], input[placeholder].

Selector Patterns

PatternDescriptionTry it
[title]Any element with a title attributeTry it
input[placeholder]Only input elements that have placeholderTry it
:not([lang])Elements that do not have a lang attributeTry it
[href="…"] familyValue matchers (=, *=, ^=, $=, ~=, |=)Try it

Attribute operator family

OperatorMeaningGuide
[attr]Attribute is presentThis page
[attr=value]Exact valueEquals
[attr*=value]Contains substringContains
[attr^=value]Starts withStarts with
[attr$=value]Ends withEnds with
[attr~=value]Space-separated wordSpace
[attr|=value]Value or value-hyphen prefixHyphen

How to Use [attr]

Name the attribute in brackets. Optionally prefix an element type.

  1. Confirm the attribute appears in the HTML (any value is fine).
  2. Write [attr] or element[attr].
  3. Add styles for those matches only.
  4. Need a specific value? Switch to an equals / contains operator.
PieceRequired?Role
Element typeNoNarrows the match (e.g. a, input)
[attr]YesRequires the attribute to be present
Attribute valueNo (for presence)Ignored by [attr]; used by other operators

Live Preview

Links with a title turn purple via a[title]. The plain link has no title, so it stays blue.

[title] demo

Only presence matters—the title text itself is not checked.

Attribute presence preview

CSS Examples

Each snippet focuses on attribute presence. Use Try It Yourself for a live editor.

Example 1 — Links With a Title

Style every anchor that has a title attribute.

CSS
a[title] {
  color: purple;
}
Try It Yourself

How It Works

  1. Name the attribute. [title] means “has title.”
  2. Limit to links. Prefix a so only anchors match.
  3. Ignore the value. Any title string still matches.
  4. Skip missing attributes. Links without title are untouched.

Example 2 — Inputs With Placeholder

Highlight form fields that show placeholder text.

CSS
input[placeholder] {
  border: 2px solid #2563eb;
}
Try It Yourself

How It Works

  1. Target inputs. Start with the input type selector.
  2. Require the attribute. [placeholder] filters to fields that declare one.
  3. Style the match. Border color marks those fields.
  4. Leave others alone. Inputs without placeholder stay default.

Example 3 — Missing Attribute With :not()

Italicize elements that do not have a lang attribute.

CSS
div[lang] {
  font-weight: bold;
}

div:not([lang]) {
  font-style: italic;
}
Try It Yourself

How It Works

  1. Match presence. div[lang] selects divs that declare lang.
  2. Invert with :not(). div:not([lang]) selects the rest.
  3. Apply different styles. Bold vs italic shows both groups.
  4. Reuse the idea. Same pattern works for required, disabled, and more.

Edge Cases & Pitfalls

Presence is simple—most mistakes come from expecting a value check.

value

[attr] does not read the value

href="" still matches [href]. Empty is still present. Use equals or other operators to filter values.

boolean

Boolean attributes count as present

<input disabled> matches [disabled] even with no value written.

case

Value case depends on the attribute

HTML attribute names are case-insensitive. Values like class / id / data-* are case-sensitive unless you add i.

quotes

Quote values with spaces

For value operators, quote strings that contain spaces or special characters: [title="Hello world"].

vs class

Prefer .class for class names

[class] only checks that class exists. To target a class name, use .name or [class~="name"].

Browser Support

Attribute selectors are browser CSS selectors. Node.js, Deno, and Bun do not evaluate them.

✓ Baseline · Widely available

[attr] presence

Core attribute selectors are Baseline Widely available. The case-sensitive s flag has more limited support than the base syntax.

99% All modern browsers
Google Chrome All
Full
Mozilla Firefox All
Full
Apple Safari All
Full
Microsoft Edge All
Full
Opera All
Full
Samsung Internet All
Full
Internet Explorer 7+
Full
[attr] Universally supported

Bottom line: Production-ready for [attr] and common value operators. Prefer i for case-insensitive value matches when needed; treat s as optional.

Key Takeaways

  • Role: match elements that have a named attribute, any value.
  • Form: [attr] or element[attr].
  • Family: use =, *=, ^=, $=, ~=, |= when the value matters.
  • Tip: invert with :not([attr]) to style missing attributes.

One line: [attr] means “this attribute exists”—not “it equals this value.”

Frequently Asked Questions

It matches any element that has the named attribute set in the HTML, no matter what the attribute’s value is.
[href] only checks that the attribute exists. [href="https://example.org"] requires an exact value match.
Yes. input[placeholder] matches only input elements that have a placeholder attribute.
Use :not([attr]), for example div:not([lang]) for divs that have no lang attribute.
In HTML, attribute names are case-insensitive. Value matching depends on the attribute: class, id, and data-* are case-sensitive unless you add the i flag.
No. It is a browser CSS selector, not a Node.js feature.

Next: [attr*=value]

Learn how to match attribute values that contain a substring.

Contains tutorial →

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.

8 people found this page helpful