


How do you perform a case-insensitive attribute match with CSS Selectors?
Jun 28, 2025 am 01:40 AMStandard CSS selectors do not support direct case-insensitive property matching, but can be implemented by combining multiple selectors or using the :is() pseudo-class. The article proposes three methods: 1. Explicit matching by listing all possible case forms such as [data-type="product"] and [data-type="Product"]; 2. Use the simplified syntax of the simplified syntax to improve readability, such as: is([data-type="product"], [data-type="Product"]); 3. For dynamic content, you can use JavaScript to add a unified class name and then define styles for this class in CSS, thereby obtaining higher flexibility and control. These solutions are applicable to different scenarios and can effectively solve the style application problems caused by inconsistent case of attribute values.
You can't directly perform a case-insensitive attribute match in standard CSS Selectors — at least not by default. But there's a workaround using the :is()
pseudo-class (or :where()
if you're managing specificity) along with multiple selectors.

Why Case-Insensitive Matching Matters
HTML attributes like data-*
or even class
are sometimes written inconsistently, especially when generated dynamically or coming from third-party systems. For example, you might see:

<div data-type="Product"></div> <div data-type="product"></div>
If your CSS only targets [data-type="product"]
, it'll miss the one with the uppercase P.
Use Multiple Attribute Selectors Together
One straightforward way to simulate case-insensitive matching is by listing all possible variations explicitly:

[data-type="product"], [data-type="Product"], [data-type="PRODUCT"] { /* styles here */ }
This works well when you know the limited set of cases you need to cover. It's simple and compatible with all modern browsers.
This approach isn't scalable if there are many possible combinations — but for small use cases, it's totally fine.
Leverage :is()
for Cleaner Syntax
To make things more concise and readable, especially when targeting multiple values ??or cases, use the :is()
pseudo-class:
:is([data-type="product"], [data-type="Product"], [data-type="PRODUCT"]) { /* styles here */ }
It does exactly the same thing as the previous example but avoids repeating the same style block multiple times.
Consider JavaScript for Dynamic Needs
If your project involves dynamic content where attribute values ??vary unpredictably in case, and you need precision styling control, consider adding a class via JavaScript instead. Then target that class in CSS.
For example:
document.querySelectorAll('[data-type]').forEach(el => { if (el.getAttribute('data-type').toLowerCase() === 'product') { el.classList.add('is-product'); } });
Then in CSS:
.is-product { /* styles here */ }
This method gives you full control and flexibility but adds a bit of scripting overhead.
So while native CSS doesn't support true case-insensitive attribute selectors, combining basic selectors or using :is()
gets you pretty close. And for more complex needs, a little JS goes a long way.
The above is the detailed content of How do you perform a case-insensitive attribute match with CSS Selectors?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Setting the size of HTML text boxes is a very common operation in front-end development. This article explains how to set the size of a text box and provides specific code examples. In HTML, you can use CSS to set the size of a text box. The specific code is as follows: input[type="text"

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to adjust WordPress themes to avoid misaligned display requires specific code examples. As a powerful CMS system, WordPress is loved by many website developers and webmasters. However, when using WordPress to create a website, you often encounter the problem of theme misalignment, which affects the user experience and page beauty. Therefore, it is very important to properly adjust your WordPress theme to avoid misaligned display. This article will introduce how to adjust the theme through specific code examples.

H5 page production process: design: plan page layout, style and content; HTML structure construction: use HTML tags to build a page framework; CSS style writing: use CSS to control the appearance and layout of the page; JavaScript interaction implementation: write code to achieve page animation and interaction; Performance optimization: compress pictures, code and reduce HTTP requests to improve page loading speed.

In Angular app, how to change the color of the icon when the mouse is hovered over it? Many developers will encounter needs when building applications using Angular...

Dynamic web element crawling problem: dealing with XPath and Class name changes, many crawler developers will encounter a difficult problem when crawling dynamic web pages: the goal...

How to solve the display problem caused by user agent style sheets? When using the Edge browser, a div element in the project cannot be displayed. After checking, I posted...

The :not() selector can be used to exclude elements under certain conditions, and its syntax is :not(selector) {style rule}. Examples: :not(p) excludes all non-paragraph elements, li:not(.active) excludes inactive list items, :not(table) excludes non-table elements, div:not([data-role="primary"]) Exclude div elements with non-primary roles.
