Mastering Precision: CSS Attribute Selectors

Go beyond classes and IDs. Learn to style elements based on their attributes for cleaner, more powerful, and semantic CSS.

Lesson ProgressStep 1 of 9
0 EXP

Welcome! Let's learn CSS Attribute Selectors. They let you style elements based on their HTML attributes.

/* Welcome to the Attribute Selector simulator! */

Basic Syntax: The Presence Selector `[attr]`

The simplest attribute selector checks only for an attribute's **presence**. The syntax `[attribute-name]` will select any element that has that attribute, regardless of its value.

/* Selects all <a> tags that have a 'target' attribute */
a[target] {
  text-decoration: underline dashed;
}

/* Selects all elements (inputs, buttons, etc.) that are disabled */
[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

This is extremely useful for applying global styles to elements in a certain state.

System Check

Which selector will target all `<img>` elements that have an `alt` attribute defined?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Attribute Aware

Use a presence selector [attr] to style an element.

🎯
Exact Match

Use an exact value selector [attr="value"] correctly.

✍️
Substring Sniper

Master the use of substring selectors (*=, ^=, $=).

Mission: Style the Document

Use attribute selectors to complete the task.
1. Give links to "document.pdf" a green background.
2. Give links with `target="_blank"` a red border.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Assemble the Selector

Drag the pieces to create a selector that targets `<a>` tags whose `href` attribute *starts with* `https`.

^=
a
]
[href
"https"
{ ... }

Challenge: Complete the Rule

Fill in the blanks to select all `input` elements that are `disabled` and give them an `opacity` of 0.5.

input[] {
  : 0.5;
}

Consult A.D.A.

Community Holo-Net

Mastering Precision: A Deep Dive into CSS Attribute Selectors

In CSS, classes and IDs are the most common ways to select elements, but they aren't always the most efficient or semantic. What if you want to style all disabled buttons? Or every link that points to a PDF? Chaining classes can get messy. This is where **attribute selectors** come in, allowing you to style elements based on their HTML attributes and values.

1. Presence Selector: `[attr]`

This is the simplest form. It selects any element that **has the specified attribute**, regardless of its value.

  • Selector: [disabled]
  • What it does: Selects any element with a `disabled` attribute (e.g., `<input disabled>`, `<button disabled>`).
  • Common Use Case: Applying a universal "disabled" style (like `opacity: 0.5` and `cursor: not-allowed`) to all form elements.
/* Style all elements that have a 'target' attribute */
a[target] {
  text-decoration: underline dashed;
}

/* Style all disabled form elements */
[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

2. Exact Value Selector: `[attr="value"]`

This selects elements where the attribute has an **exact value**. This is case-sensitive by default.

  • Selector: [type="submit"]
  • What it does: Selects only elements with `type="submit"`. It will *not* match `type="text"` or `type="SUBMIT"`.
  • Common Use Case: Styling all submit buttons differently from other input types without adding a class.
/* Style only submit buttons */
input[type="submit"] {
  background-color: #2563eb;
  color: white;
  border: none;
}

/* Style links that open in a new tab */
a[target="_blank"] {
  /* Add an icon to indicate new tab */
  content: ' &rarr;';
}

3. Substring Selectors: `*=`, `^=`, `$=`

These are incredibly powerful for matching parts of a value.

Contains `[attr*="value"]`

Selects elements if the value **contains** the substring anywhere.

Example: `[class*="icon-"]` would match `class="icon-menu"` and `class="header icon-user"`.

Starts With `[attr^="value"]`

Selects elements if the value **starts with** the substring.

Example: `[href^="https"]` matches all secure links. `[href^="mailto:"]` matches all email links.

Ends With `[attr$="value"]`

Selects elements if the value **ends with** the substring.

Example: `[href$=".pdf"]` matches links to PDF files. `[src$=".png"]` matches PNG images.

4. List Selectors: `~=` and `|=`

These are more specific than substring matches and are designed for lists.

  • `[attr~="value"]` (Word Match): Selects if the value is a **space-separated list** that contains the exact word. `[class~="btn"]` matches `class="btn btn-primary"` but *not* `class="btn-primary"`.
  • `[attr|="value"]` (Hyphen Match): Selects if the value is *exactly* `value` or *starts with* `value` immediately followed by a hyphen. Primarily used for language codes, like `[lang|="en"]` matching `lang="en"` and `lang="en-US"`.

5. Case Insensitivity: The `i` Flag

By default, all value matching is case-sensitive. You can make it **case-insensitive** by adding an `i` (or `I`) before the closing bracket.

/* This will match type="text", type="TEXT", type="Text", etc. */
input[type="text" i] {
  border: 1px solid #ccc;
}

6. Practical Magic: `data-*` Attributes

This is where attribute selectors truly shine. You can create custom HTML attributes (e.g., `data-state`, `data-tooltip`) and style them directly. This lets you change your CSS based on application state, without adding/removing classes with JavaScript.

<div class="modal" data-state="closed">...</div>
<button data-tooltip="Click to save">Save</button>

/* CSS */
.modal[data-state="closed"] {
  display: none;
}
.modal[data-state="open"] {
  display: block;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  /* ...styles for a tooltip... */
}
Key Takeaway: Attribute selectors add a new layer of precision to your CSS. They are more semantic than utility classes and more powerful than simple tag selectors. Use them to style forms, file links, and custom `data-*` states to write cleaner, more maintainable code.

CSS Attribute Selector Glossary

Attribute Selector
A type of CSS selector that targets elements based on the presence or value of their HTML attributes. The syntax is enclosed in square brackets `[...]`.
`[attr]` (Presence)
Selects any element that has the `attr` attribute, regardless of its value. Example: `[disabled]` selects all disabled elements.
`[attr="value"]` (Exact Value)
Selects elements where `attr` has the *exact* value. This is case-sensitive. Example: `[type="submit"]`.
`[attr~="value"]` (Word Match)
Selects elements where `value` is one of a space-separated list of words. Example: `[class~="btn"]` matches `class="btn btn-primary"`.
`[attr|="value"]` (Hyphen Match)
Selects elements where `attr` is exactly `value` or starts with `value` followed by a hyphen. Example: `[lang|="en"]` matches `lang="en"` and `lang="en-US"`.
`[attr^="value"]` (Starts With)
Selects elements where the `attr` value *begins with* `value`. Example: `[href^="https"]`.
`[attr$="value"]` (Ends With)
Selects elements where the `attr` value *ends with* `value`. Example: `[href$=".pdf"]`.
`[attr*="value"]` (Contains)
Selects elements where the `attr` value *contains* the substring `value` anywhere. Example: `[class*="icon-"]`.
`[attr="value" i]` (Case-Insensitive)
A flag added before the closing `]` that makes the value comparison case-insensitive. Example: `[type="text" i]` matches `type="TEXT"`.
`data-*` Attributes
Custom attributes (e.g., `data-state`) that are valid HTML and can be powerfully targeted by attribute selectors, often for JavaScript interactivity or CSS-only state management.
Specificity
Attribute selectors (like classes and pseudo-classes) have a specificity value of (0,1,0). This means they are more specific than tag selectors (0,0,1) but less specific than ID selectors (1,0,0).

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS3 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!