CSS wildcard class selectors: [class*=], [class^=] and [class$=]
Codemia Knowledge Hub ·
CSS has no wildcard for class names. The * character only appears in the universal selector, which selects every element. To match classes by part of their name, use attribute selectors on the class attribute, such as [class*="btn"] (contains), [class^="icon-"] (starts with) or [class$="-btn"] (ends with).
Attribute selectors that work like wildcards
- Attribute presence and value selectors:
[attr]: elements that have the attribute, whatever its value.[attr="value"]: elements whose attribute equals the value.
- Substring matching selectors:
[attr^="value"]: the attribute value begins with "value".[attr$="value"]: the attribute value ends with "value".[attr*="value"]: the attribute value contains the substring "value".
- Whole word in a space-separated list:
[attr~="value"]: useful for classes, since it matches when the attribute is a list of space-separated values and one of them is exactly "value".
- Start of a dash-separated value:
[attr|="value"]: mainly used for matching language codes and subcodes.
Examples with class names
- Class containing
btn:
This matches .btn, .my-btn and any other class containing the letters btn.
- Class beginning with
icon-:
Handy for font icons such as .icon-home and .icon-user.
- Class ending with
-btn:
Matches .action-btn, .submit-btn and so on.
Quick reference
| Use Case | Selector | Description | Example |
| Matching a substring in the class | [class*="part"] | Selects all elements where class contains "part". | <div class="apartment"></div> matches |
| Matching the beginning of a class | [class^="start"] | Selects all elements where the class starts with "start". | <div class="starter"></div> matches |
| Matching the end of a class | [class$="end"] | Selects all elements where class ends with "end". | <div class="friend"></div> matches |
| Matching a whole word in the class | [class~="word"] | Selects all elements where class contains the whole word "word". | <div class="one word two"></div> matches |
Performance and specificity
- Performance: attribute selectors, especially substring ones, can be slower than class or ID selectors, because the browser must test each element's attribute value against the pattern.
- Specificity: they carry the specificity of an attribute selector, lower than an ID but higher than an element selector, so more specific or
!importantdeclarations can override them.
Related reading
- Will a browser give an iframe a separate thread for JavaScript?
- window.onbeforeunload not working on the iPad?
- window.onload vs document.onload
- window.onload vs $(document).ready()
- Winston log files not always saved while using process.exit - node js
- With a Promise, why do browsers return a reject twice but not a resolve twice?
- Working with $scope.$emit and $scope.$on
- Wrapping a callback-based class to an async one
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.