- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Attribute Selector
The attribute selector in CSS is a highly efficient way to target HTML elements based on the specific attributes they carry. Instead of cluttering your HTML with endless classes, you can hook into existing data like link destinations, input types, or custom data attributes to apply your styles. This approach is particularly useful when working with CMS-generated content where you might not have direct control over the HTML classes.
Presence and Value Selectors:
[attr]: Targets elements that possess the specified attribute, regardless of what its value actually is.[attr=value]: Targets elements where the attribute matches the specified value exactly.
[attr=value] selector to style different types of form inputs (like [type="checkbox"] or [type="radio"]) without needing to add unique classes to every single input field.
Substring Value Selectors:
These selectors allow for "fuzzy" matching, giving you more flexibility when you only know part of an attribute's value.
[attr^=value]: The "starts with" selector. It targets elements whose attribute value begins with the specified string. This is great for identifying secure links (https://).[attr$=value]: The "ends with" selector. This is frequently used to add icons to links based on file extensions like.pdfor.doc.[attr*=value]: The "contains" selector. It selects elements if the specified value appears anywhere within the attribute string.
a[href$=".pdf"]::after to automatically add a PDF icon after every link that points to a document. This improves user experience by letting users know what to expect before they click.
Specificity Value Selectors:
[attr|=value]: Matches elements with an attribute value that is exactly "value" or starts with "value" followed immediately by a hyphen (e.g.,en-US). This is primarily used for language code matching.[attr~=value]: Targets elements where the attribute value is a space-separated list (like a class list), and one of those words matches the value exactly.
*= with ~=. Remember that ~= looks for a specific whole word in a list, while *= will match a string even if it is just a part of a larger word.
Case Sensitivity:
[attr=value i]: By default, attribute selectors are case-sensitive in some contexts (depending on the document language). Adding anibefore the closing bracket forces the selector to match the value case-insensitively.
Here’s a real-world example of how these selectors can be applied to a modern web interface:
CSS
/* 1. Highlight links that are missing an 'alt' attribute for accessibility auditing */
img:not([alt]) {
border: 5px solid red;
}
/* 2. Target specific input types for consistent form styling */
input[type="email"] {
border-bottom: 2px solid blue;
}
/* 3. Style external links differently using the "starts with" selector */
a[href^="https://"] {
font-weight: bold;
color: #1a73e8;
}
/* 4. Target custom data attributes used in JavaScript components */
div[data-status="active"] {
background-color: #e6fffa;
border-left: 4px solid #38b2ac;
}
/* 5. Case-insensitive matching for file extensions */
a[href$=".jpg" i] {
border-bottom: 1px dashed orange;
}
In these examples:
- The
img:not([alt])selector is a life-saver for developers doing accessibility checks, visually flagging images that screen readers might skip. - The
input[type="email"]selector ensures that only email fields get the blue underline, leaving text or password fields untouched. - Using
data-statusis a "clean" way to bridge the gap between your CSS and your logic (JavaScript), as it allows you to change styles based on state without toggling CSS classes manually.