CSS Attribute Selectors
Target elements based on their attributes and values. Create precise, semantic selectors that adapt to your HTML structure dynamically.
How do CSS attribute selectors work?
Attribute selectors match elements by their attributes and values, written in square brackets. [disabled] matches when the attribute exists, [type="email"] matches an exact value, [href^="https"] matches values that start with a string, [href$=".pdf"] ones that end with it, and [class*="btn"] ones that contain it.
input[type="email"] { }
a[href^="https://"] { } /* starts with */
a[href$=".pdf"] { } /* ends with */
[class*="icon-"] { } /* contains */
[lang|="en"] { } /* "en" or "en-…" */
[data-state="open"] { }Interactive Attribute Selectors Demo
Code Editor
<a href="https://example.com">External https link</a><br>
<a href="/files/guide.pdf">PDF download</a><br>
<a href="mailto:hi@example.com">Email link</a><br>
<input type="email" placeholder="email input"> <input type="text" placeholder="text input">Preview
Basic Attribute Selectors
Attribute Presence and Exact Match
/* Select elements that have the attribute */
[title] {
border-bottom: 1px dotted #666;
cursor: help;
}
/* Select elements with exact attribute value */
[type="email"] {
border-left: 4px solid #3b82f6;
background: #eff6ff;
}
[type="password"] {
border-left: 4px solid #dc2626;
background: #fef2f2;
}
/* Multiple attribute selectors */
input[type="text"][required] {
border: 2px solid #f59e0b;
background: #fffbeb;
}Target elements by attribute presence or exact value matches.
Form Element Styling
/* Style by input type */
[type="search"] {
border-radius: 20px;
padding-left: 2rem;
}
/* Required fields */
[required] {
box-shadow: 0 0 0 2px rgba(239,68,68,0.2);
}
/* Disabled inputs */
[disabled] {
opacity: 0.5;
cursor: not-allowed;
}
/* Readonly inputs */
[readonly] {
background: #f3f4f6;
border-style: dashed;
}Style form elements based on their type and state
Accessibility Attributes
/* ARIA attributes */
[aria-expanded="true"] {
background: #dbeafe;
}
[aria-hidden="true"] {
visibility: hidden;
}
/* Role-based styling */
[role="button"] {
cursor: pointer;
user-select: none;
}
[tabindex="-1"] {
outline: none;
}Style elements based on accessibility attributes
Partial Value Matching
Contains, Starts With, Ends With
/* Contains substring */
[href*="github"] {
color: #1f2937;
font-weight: bold;
}
[class*="btn"] {
padding: 0.5rem 1rem;
border-radius: 4px;
}
/* Starts with prefix */
[href^="https"] {
color: #059669;
}
[href^="mailto"] {
color: #dc2626;
}
[id^="section-"] {
scroll-margin-top: 4rem;
}
/* Ends with suffix */
[href$=".pdf"] {
background: #fef3c7;
padding: 0.25rem;
}
[src$=".svg"] {
filter: drop-shadow(0 1px 2px rgba(0,0,0,0.1));
}Use wildcards to match parts of attribute values.
Link Type Styling
/* External links */
[href^="http"]::after {
content: " ↗";
font-size: 0.8em;
}
/* Email links */
[href^="mailto"]::before {
content: "✉ ";
}
/* Phone links */
[href^="tel"]::before {
content: "📞 ";
}
/* Download links */
[href$=".zip"]::after,
[href$=".rar"]::after {
content: " 📦";
}Add visual indicators for different link types
File Type Icons
/* PDF files */
[href$=".pdf"]::after {
content: " 📄";
color: #dc2626;
}
/* Image files */
[href$=".jpg"]::after,
[href$=".png"]::after,
[href$=".gif"]::after {
content: " 🖼";
}
/* Video files */
[href$=".mp4"]::after,
[href$=".avi"]::after {
content: " 🎥";
}
/* Document files */
[href$=".doc"]::after,
[href$=".docx"]::after {
content: " 📝";
}Automatically add icons based on file extensions
Data Attributes and Custom Selectors
Data Attribute Styling
/* Status-based styling */
[data-status="success"] {
background: #dcfce7;
border-left: 4px solid #16a34a;
color: #15803d;
}
[data-status="warning"] {
background: #fef3c7;
border-left: 4px solid #d97706;
color: #92400e;
}
[data-status="error"] {
background: #fecaca;
border-left: 4px solid #dc2626;
color: #b91c1c;
}
/* Theme switching */
[data-theme="dark"] {
background: #1f2937;
color: #f9fafb;
}
/* Size variants */
[data-size="large"] {
font-size: 1.25rem;
padding: 1rem 1.5rem;
}Use data attributes for semantic component styling.
Language Attributes
/* Language-specific styling */
[lang="en"] {
font-family: Georgia, serif;
}
[lang="ar"] {
direction: rtl;
text-align: right;
}
[lang="ja"] {
font-family: "Hiragino Sans", sans-serif;
line-height: 1.8;
}
/* Language prefix matching */
[lang|="en"] {
/* Matches en, en-US, en-GB, etc. */
quotes: """ """;
}Adapt styling based on content language
State Management
/* Component states */
[data-loading="true"] {
opacity: 0.6;
pointer-events: none;
}
[data-expanded="true"] {
max-height: 500px;
overflow: visible;
}
[data-visible="false"] {
opacity: 0;
transform: translateY(-10px);
}
/* Priority levels */
[data-priority="high"] {
border-left: 4px solid #dc2626;
font-weight: bold;
}Manage component states with data attributes
Advanced Attribute Techniques
Case-insensitive and Word Matching
/* Case-insensitive matching (use 'i' flag) */
[href*="GITHUB" i] {
/* Matches github, GITHUB, GitHub, etc. */
color: #1f2937;
}
/* Word matching with ~= */
[class~="button"] {
/* Matches "button", "btn button", "button primary" */
display: inline-block;
padding: 0.5rem 1rem;
}
/* Language prefix matching with |= */
[lang|="en"] {
/* Matches "en", "en-US", "en-GB" */
font-family: "Times New Roman", serif;
}
/* Complex combinations */
input[type="text"][data-validate*="email" i]:not([disabled]) {
background-image: url('email-icon.svg');
}Use advanced matching techniques for flexible selectors.
Dynamic Content with attr()
/* Display attribute content */
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
background: #1f2937;
color: white;
padding: 0.5rem;
border-radius: 4px;
}
/* Show URL for print */
@media print {
a[href]::after {
content: " (" attr(href) ")";
font-size: 0.8em;
color: #666;
}
}
/* Display alt text for broken images */
img::after {
content: attr(alt);
display: block;
text-align: center;
color: #666;
border: 1px dashed #ccc;
padding: 1rem;
}Use attr() function to display attribute values as content.
Best Practices & Performance
Best Practices
- • Use specific attribute selectors over broad ones
- • Combine with element selectors for better performance
- • Use data attributes for component states
- • Avoid overly complex attribute selectors
- • Consider browser support for newer features
- • Use semantic HTML attributes when possible
Creative Uses
- • Style form validation states
- • Create theme switchers
- • Build component state management
- • Add file type indicators
- • Internationalization styling
- • Print-friendly link URLs
CSS Attribute Selectors — step by step
[attr] and [attr=value]
Select elements that have an attribute with [href], or match an exact value with [type='submit']. No quotes needed for single-word values.
🎯 Quick Check
Which attribute selector matches elements whose value ENDS with a specific string?
CSS Attribute Selectors: summary
Key points
- Attribute selectors have the same specificity as a class.
- Add
ibefore the closing bracket for case-insensitive matching:[href$=".PDF" i]. - They pair well with
data-*attributes and ARIA states, for example[aria-expanded="true"].
Frequently asked questions
›How do I style links to PDF files?
Use a[href$=".pdf"], which matches links whose URL ends in .pdf.
›Can I select by data attributes?
Yes, like any attribute: [data-theme="dark"] or simply [data-active].