CSS Takes a Practical Step Forward With Class Prefix Selector

The CSS Working Group has resolved to add a class prefix selector, written like .btn-*, to Selectors Level 5. It would let one rule target every class sharing a hyphen-separated prefix without extra base classes or slow attribute selectors.
CSS Takes a Practical Step Forward With Class Prefix Selector
CSS Takes a Practical Step Forward With Class Prefix Selector

CSS is getting a dedicated way to target every class that shares a prefix. At its face-to-face meeting in Berlin in August, the CSS Working Group resolved to add a class prefix selector to the Selectors Level 5 specification, Chrome developer relations engineer Bramus wrote on bram.us. The feature exists only in spec text for now and is not yet available in any browser.

The Problem It Solves

Developers commonly group classes with prefixes such as .btn-primary, .btn-secondary and .btn-danger. Styling them all today means listing each one, adding a separate .btn base class to the markup, or using substring-matching attribute selectors like [class*=” btn-“]. Those attribute selectors perform badly. In a benchmark Bramus ran, a regular class selector managed more than 6,000 runs per second while the attribute selector dropped as low as 328, almost 20 times slower.

With the new selector, .btn-* matches any class that begins with the hyphen-separated prefix btn-. The idea was first pitched by Lea Verou in 2024 in CSSWG issue #10001 and was championed at the meeting by Verou and Tab Atkins-Bittner.

Deliberate Limits

As currently specified, the selector matches only classes with at least one character after the prefix, and that character can’t be another hyphen, so .foo-* would not match class=”foo-” or class=”foo–“. The working group is still discussing whether that default should change.

A separator is required. Arbitrary prefixes such as .foo* won’t be allowed, partly because they could accidentally overselect (.foo* would match .footer) and partly because browsers bucket class selectors for fast matching, which arbitrary wildcards would defeat. Wildcards in the middle of a selector, such as .card-*-primary, are also ruled out. Only hyphens are supported for now, though other separators such as underscores could be added if developers ask for them.

For teams with utility classes and design systems, the selector would shrink both stylesheets and HTML while avoiding the performance cost of attribute matching. Bramus is asking developers to send feedback on the in-progress design to the CSS Working Group through issue #10001 while the spec can still change.

Subscribe for Updates

DevWebPro Newsletter

The DevWebPro Email Newsletter is a must-read for web and mobile developers, designers, agencies, and business leaders. Stay updated on the latest tools, frameworks, UX trends, and best practices for building high-performing websites and apps.

By signing up for our newsletter you agree to receive content related to ientry.com / webpronews.com and our affiliate partners. For additional information refer to our terms of service.

Notice an error?

Help us improve our content by reporting any issues you find.

Get the WebProNews newsletter delivered to your inbox

Get the free daily newsletter read by decision makers

Subscribe
Advertise with Us

Ready to get started?

Get our media kit

Advertise with Us