
Introduction
When you’re building modern UIs at scale, class names can get messy fast. From CMS-generated content to utility-first frameworks, your codebase may end up full of deeply patterned, semi-structured classes. That’s where CSS attribute selectors come in. If you’re new to CSS, our article What is css? covers the basics of the technology.
With that said, when talking about regular expressions, CSS doesn’t support them in the most common way. It does, however include regex-style selectors like ^=, $=, and *= that let you target elements based on the beginning, end, or partial match of an attribute value.
If you’re working on large-scale custom-coded projects, or just implementing reusable components—knowing how to use these selectors well can save you time, reduce bloat, and keep your styles DRY.
What Is RegEx and Why It’s Useful
Regular expressions (RegEx) are patterns used to match character combinations in strings. They’re widely used in programming for validation, search, and complex pattern matching. While CSS doesn’t support full RegEx, understanding the concept helps you appreciate the power of CSS’s partial match selectors. Read our guide to learn how the full RegEx is implemented in Java.
In CSS, RegEx-style logic enables developers to target multiple elements with similar attributes—making stylesheets more scalable and efficient.
Regex-Like Selectors Available in CSS
CSS provides three key attribute selectors that mimic regex behavior:
- ^=: Matches values that start with a specific string
- $=: Matches values that end with a specific string
- *=: Matches values that contain a specific string anywhere
Side-by-side Comparison: Regex vs CSS
Purpose | Regex Syntax | CSS Selector |
Starts with | ^text | [class^=”text”] |
Ends with | text$ | [class$=”text”] |
Contains (anywhere) | text | [class*=”text”] |
Real-World Examples
/* Style all buttons that start with “btn-” */
[class^=”btn-“] {
padding: 1rem;
font-weight: 600;
}
/* Style anything ending in “-active” */
[class$=”-active”] {
opacity: 1;
pointer-events: auto;
}
/* Target elements containing “featured” */
[class*=”featured”] {
border: 2px solid gold;
}
These patterns are especially helpful when working with BEM, Tailwind-style utilities, or dynamic class names generated by a CMS.
Practical Use Cases in Custom Projects
Attribute selectors are especially valuable when you’re working with large codebases, CMS-generated markup, or reusable components. Here’s how they come in handy:
1. Styling CMS-Generated Content
In many custom environments—such as static site generators, headless CMSs, or even legacy systems—you’ll often find repetitive class patterns like product-card-1, product-card-2, etc. Instead of targeting each class individually:
[class^=”product-card-“] {
border-radius: 0.5rem;
background: #f9f9f9;
}
This targets all elements whose class starts with product-card-, no matter how many variations exist.
2. Utility-First Styling
Utility-based codebases often generate class names like text-lg, text-md, text-sm, etc. Want to tweak the line height across all of them?
[class*=”text-“] {
line-height: 1.5;
}
This method works beautifully when scaling or retrofitting a design system.
3. Data Attribute Logic
Remember, these selectors aren’t just for class. You can use them on any attribute. For example, conditional rendering via data attributes:
[data-status^=”inactive”] {
opacity: 0.5;
pointer-events: none;
}
This is useful when you’re toggling states without JS—especially in pre-rendered or low-interactivity environments.
4. Prototyping in Web Builders
Even if you’re using a visual builder like Webflow or WordPress, understanding how these selectors behave is crucial — especially for any Webflow development company building scalable, class-driven systems that later rely on JavaScript or dynamic logic.
The more consistent your naming, the more power these selectors offer.
Pitfalls and Best Practices
While regex-style selectors in CSS are powerful, they’re not foolproof. Here’s what to watch out for, and how to use them wisely.
Avoid Over-Reliance in Unpredictable Environments
Attribute selectors like [class^=] or [class*=] depend entirely on predictable class naming. If your project involves external libraries, plugins, or contributions from multiple developers, class names can vary — making these selectors brittle and error-prone.
Example: A change from btn-primary to primary-btn would break a [class^=”btn”] selector.
Use Them in Controlled Systems
These selectors shine when used within systems that follow naming conventions like BEM (Block-Element-Modifier), utility-first frameworks, or auto-generated classes from CMSs that you control.
They’re ideal for:
- Style guides and design systems
- CMS templates where you name the classes
- Custom design tokens in apps
Don’t Replace Semantic Structure
Regex-style selectors aren’t a substitute for thoughtful HTML structure or semantic class naming. They’re a complement — great for shortcuts or batch styling, not as the foundation of your CSS architecture.
Consider Performance
While modern browsers handle attribute selectors efficiently, excessive use of complex selectors (especially *=) can lead to unnecessary DOM scanning — which might slow down rendering in large documents.
When to Use JavaScript Instead
If you find yourself needing logic like:
- “Starts with A, doesn’t contain B”
- “Ends in C but only if inside a container with class X”
…then JavaScript (or a preprocessor like PostCSS) is your best bet.
Regex-style selectors are best used deliberately — in systems where structure is predictable, and your team sticks to the rules.
Summary
Regex-style selectors in CSS—^=, $=, and *=—aren’t true regular expressions, but they offer a lightweight, powerful way to match patterns in class names and attributes.
They shine in projects with clear naming conventions, such as scalable design systems or CMS-generated layouts, helping you apply styles more efficiently without cluttering your codebase.
Just remember:
- They’re best used when naming is predictable
- They can break if class names change
- More advanced logic requires JavaScript or build tools
For frontend developers or teams working with frameworks or platforms like Webflow, mastering these selectors unlocks cleaner, more maintainable styling workflows.
Ready to Go Deeper?
If regex-style selectors caught your interest, it might be time to strengthen your foundation.
CSS works best when paired with a strong understanding of the basics — and that means getting familiar with the full trio: HTML, CSS, and JavaScript. Each technology plays a unique role in front-end development, and mastering them helps you build more powerful, scalable, and responsive websites.
Here are some beginner-friendly guides to get you started:
- Learn HTML: Best HTML Tutorials & eBooks (PDFs) — a solid roundup of free and paid resources.
- Learn CSS: Top Web Design Books for Beginners — learn layout, responsive design, and more.
- Learn JavaScript: 5 Best JavaScript Books for Beginners — get started with the scripting language that powers interactivity on the web.
Whether you’re just starting out or leveling up, these resources will give you the tools you need to write cleaner, smarter, and more maintainable front-end code.