CSS Portal

CSS [attribute operator value s] Selector

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The [attribute operator value s] selector is a powerful CSS tool that allows authors to target elements based on the presence and value of their attributes. Unlike simple attribute selectors that only check for the existence of an attribute, this selector uses an operator to match the attribute’s value against a specific pattern, giving you more precision and flexibility when styling elements. It can be used to apply styles to elements dynamically without requiring additional classes or IDs.

This selector is particularly useful when working with structured content or forms. For example, if you want to style all <input> elements of type "text", you could use [type="text"]. However, with the [attribute operator value s] selector, you can perform more advanced matching, such as selecting elements whose attributes start with, end with, or contain certain values. This enables designs that adapt to varying attribute values without hardcoding multiple selectors.

A common application is for styling links or buttons dynamically. Suppose you have several <a> tags with a data-role attribute that varies, such as data-role="admin" or data-role="guest". You can style all elements starting with "admin" using the appropriate operator in this selector. Combined with other selectors like .class or pseudo-classes, it allows for sophisticated targeting strategies.

Additionally, [attribute operator value s] can enhance responsiveness by controlling styles for elements based on attributes like lang, title, or aria-*. For instance, you might apply different font sizes or colors depending on a language attribute, linking to CSS properties like color or font-size. This selector therefore helps maintain semantic HTML structure while still providing rich, conditional styling.

In practice, its flexibility means that developers can write CSS rules that adapt to changes in the HTML without needing extra markup. By leveraging the operators thoughtfully, such as ^= (starts with), $= (ends with), and *= (contains), this selector can target a wide array of elements efficiently, making stylesheets cleaner and more maintainable.

Syntax

[attribute=value i] { css declarations; }

Example

<div class="container">
<div data-type="alert">This is a lowercase alert (Matches).</div>
<div data-type="Alert">This is a capitalized Alert (Ignored).</div>
<div data-type="ALERT">This is an uppercase ALERT (Ignored).</div>
<div data-type="alert">Another lowercase alert (Matches).</div>
</div>
/* Only matches 'alert' exactly. 'Alert' or 'ALERT' will be ignored. */
[data-type="alert" s] {
background-color: #ffe5e5;
border: 2px solid #ff4d4d;
color: #b30000;
padding: 10px;
margin-bottom: 8px;
font-weight: bold;
}

/* General styling for visibility */
.container {
font-family: sans-serif;
line-height: 1.5;
}

div:not([data-type="alert" s]) {
color: #888;
padding: 10px;
}

Browser Support

The following information will show you the current browser support for the CSS [attribute operator value s] selector. Hover over a browser icon to see the version that first introduced support for this selector.

This CSS [attribute operator value s] selector is supported in some modern browsers, but not all.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 2nd January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!