Attribute Selectors
Classes and IDs are the selectors most developers reach for, but they are not the only way to target elements. Every HTML attribute is fair game. Attribute selectors let you style elements based on whether an attribute exists, what its value is, or whether the value starts with, ends with, or contains a particular string. This means you can style external links differently from internal ones, highlight required form fields, or theme an entire component using a single data- attribute, all without adding extra classes or writing any JavaScript.
Attribute selectors have been part of CSS since CSS 2.1, so browser support is universal. The substring matchers (^=, $=, *=) were introduced in Selectors Level 3, and the case sensitivity modifiers (i and s) arrived in Selectors Level 4. All of these features enjoy full support in every modern browser.
The Syntax at a Glance
Every attribute selector is wrapped in square brackets. The simplest form, [attr], matches any element that has that attribute at all, regardless of its value. From there you can narrow the match by comparing the attribute value against a string. Here is the full set of matchers:
/* Presence: has the attribute at all */
[disabled] { opacity: 0.5; }
/* Exact match */
[type="email"] { border-color: teal; }
/* Starts with */
[href^="https"] { padding-left: 1.2em; }
/* Ends with */
[href$=".pdf"] { background: url("pdf-icon.svg") no-repeat left; }
/* Contains */
[class*="grid"] { display: grid; }
/* Whitespace-separated word match */
[class~="active"] { font-weight: bold; }
/* Exact or hyphen-separated prefix (en-US, es-ES, etc.) */
[lang|="en"] { quotes: "\201C" "\201D"; }
The first three substring matchers are the ones you will use most often. The ^= (starts with) matcher is perfect for protocol detection in links. The $= (ends with) matcher lets you identify file types by extension. The *= (contains) matcher is the broadest and catches any occurrence of the substring anywhere in the value. The remaining two (~= and |=) serve more specialized roles: ~= matches a complete word in a space-separated list, and |= was originally designed for language subtag matching.
Practical Pattern: Styling Links by Destination
One of the most common and satisfying uses of attribute selectors is styling links based on where they point. You can add an external link icon to outbound URLs, flag downloadable files, or visually distinguish mailto links, all based on the href value alone:
/* External links that leave your site */
a[href^="http"]:not([href*="yourdomain.com"]) {
color: var(--link-external);
}
/* Email links */
a[href^="mailto:"] {
color: var(--link-email);
}
/* PDF downloads */
a[href$=".pdf"] {
color: var(--link-pdf);
}
/* Image files */
a[href$=".png"],
a[href$=".jpg"],
a[href$=".webp"] {
color: var(--link-image);
}
Notice how this approach does not require you to add any classes to your markup. The selector reads the destination directly from the href attribute and styles the link accordingly. If the URL changes, the styling follows automatically.
Practical Pattern: Form Inputs Without Extra Classes
Forms are full of attributes that distinguish one input from another. The type, required, disabled, and readonly attributes all carry meaning, and attribute selectors let you style based on that meaning directly:
/* All text-like inputs */
input[type="text"],
input[type="email"],
input[type="url"],
input[type="search"] {
border: 1px solid var(--border);
border-radius: 0.25rem;
padding: 0.5rem;
}
/* Required fields get a subtle left border */
input[required] {
border-left: 3px solid var(--accent);
}
/* Disabled fields are dimmed */
input[disabled] {
opacity: 0.5;
cursor: not-allowed;
}
This pattern keeps your HTML clean. Instead of adding class="required-field" alongside the required attribute, you let the attribute do double duty. The HTML already describes the behavior; the CSS simply reads it.
Data Attributes: Styling Without JavaScript
Custom data- attributes open up a particularly powerful pattern. You can embed state, configuration, or variant information directly in your HTML, then let CSS respond to it. This is useful for component theming, layout control, and toggling visual states without writing any JavaScript at all.
Consider a set of cards where each card has a status. Rather than toggling classes through JavaScript, you set a data-status attribute on the element and let CSS handle the rest:
<div class="card" data-status="active">...</div>
<div class="card" data-status="pending">...</div>
<div class="card" data-status="archived">...</div>
.card[data-status="active"] { border-color: green; }
.card[data-status="pending"] { border-color: orange; }
.card[data-status="archived"] { opacity: 0.6; }
MDN has an excellent guide on using data attributes that covers the HTML side, the JavaScript dataset API, and the CSS attribute selector patterns that connect them.
You can also use the attr() CSS function to pull a data attribute value into generated content. This is limited to the content property, but it can be handy for displaying labels, badges, or status text without duplicating that information in the visible markup:
.card[data-status]::after {
content: attr(data-status);
text-transform: uppercase;
font-size: 0.75em;
}
Case Sensitivity Modifiers
By default, attribute value matching in HTML follows the rules of the attribute itself. Most attribute values (like class, id, and data-*) are case-sensitive, meaning [data-status="Active"] will not match data-status="active". The type attribute on inputs is a notable exception and is matched case-insensitively per the HTML specification.
Selectors Level 4 introduced two modifier flags you can place inside the closing bracket to override the default behavior:
/* Case-insensitive match (the "i" flag) */
a[href$=".PDF" i] {
/* Matches .pdf, .PDF, .Pdf, etc. */
}
/* Case-sensitive match (the "s" flag) */
[data-code="ABC" s] {
/* Only matches exactly "ABC", not "abc" or "Abc" */
}
The i flag is the one you will reach for more often. It is especially useful when you cannot guarantee the casing of user-generated content or CMS output. The s flag exists mostly for completeness, since most attribute values are already case-sensitive by default.
Live Demo
The demo below shows attribute selectors styling a list of links based solely on their href values. Each link gets a colored tag and label generated from CSS alone. No classes distinguish these links from one another:
Every label and color in the demo above comes from attribute selectors and the ::before pseudo-element. The CSS reads each link's href and generates the appropriate tag. Here is the key portion of the CSS behind it:
a[href^="https"]::before {
content: "external";
background: #dbeafe;
color: #1e40af;
}
a[href^="mailto:"]::before {
content: "email";
background: #fce7f3;
color: #9d174d;
}
a[href$=".pdf"]::before {
content: "pdf";
background: #fee2e2;
color: #991b1b;
}
Specificity and Combining Selectors
An attribute selector carries the same specificity weight as a class selector: (0, 1, 0). That means [type="text"] and .text-input are equal in the cascade. You can combine attribute selectors with each other, with classes, or with type selectors to increase specificity when needed:
/* Type + attribute: (0, 1, 1) */
input[required] { ... }
/* Two attribute selectors: (0, 2, 0) */
[type="text"][data-size="large"] { ... }
/* Class + attribute: (0, 2, 0) */
.form-field[disabled] { ... }
This is worth keeping in mind when you combine attribute selectors with the modern pseudo-classes you learned about on the Modern Selectors page. Remember that :is() takes on the specificity of its most specific argument, while :where() contributes zero specificity. Wrapping an attribute selector in :where() can be a useful way to create low-priority defaults that are easy to override.
Resources
Use these references to deepen your understanding and find ideas for your teaching video:
- MDN: Attribute Selectors – the definitive reference with syntax tables and browser notes
-
MDN: Using Data Attributes – covers
data-*from the HTML side, the JavaScriptdatasetAPI, and CSS selection - CSS-Tricks: Attribute Selectors – clear walkthrough with practical examples
- CSS-Tricks: A Complete Guide to Data Attributes – in-depth guide covering HTML, JavaScript, and CSS patterns
-
W3C Selectors Level 4 Specification – the official spec including the
iandscase sensitivity flags
Teaching Video Checklist
You do not need to follow this order, but your teammates should come away understanding each of these concepts:
-
The basic syntax: presence (
[attr]), exact match ([attr="val"]), and the three substring matchers (^=,$=,*=) -
At least one working demo that styles links based on their
href(external vs. internal, file types, or protocol) -
How
data-*attributes can drive visual state or theming from CSS without JavaScript -
The
icase-insensitivity flag and when it is useful - How attribute selector specificity compares to class selectors and how they can be combined