wildcard * in CSS for classes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In CSS, wildcards are not directly a part of the syntax, but there are selector patterns that mimic wildcard-like behavior. These are often referred to using the '*' character in discussions about CSS, though it only specifically appears in the universal selector, which selects all elements. When it comes to classes, CSS wildcard matching can be leveraged primarily through attribute selectors. These selectors allow the targeting of elements based on partial attribute and class name matches.
Understanding CSS Attribute Selectors
CSS offers several types of attribute selectors that can be useful for matching class names in a way similar to using wildcards:
- Attribute presence and value selectors:
[attr]: Selects elements with the specified attribute, regardless of its value.[attr="value"]: Selects elements with the specified attribute and value.
- Substring matching attribute selectors:
[attr^="value"]: Matches every element whose attribute value begins with "value".[attr$="value"]: Matches every element whose attribute value ends with "value".[attr*="value"]: Matches every element whose attribute value contains the substring "value".
- Specifying word "value" within a whitespace-separated list of words in an attribute:
[attr~="value"]: Useful for classes as it matches any element whose attribute value is a list of space-separated values, one of which is exactly "value".
- Matching the starting value within a dash-separated list in an attribute (commonly used in language subcode matches):
[attr|="value"]: This is ideally used for matching elements based on language codes.
Practical Examples
Here are some practical examples showing how these can be used with class selectors in CSS:
- Selecting all elements with a class that includes 'btn':
This would target .btn, .my-btn, button, etc.
- Selecting elements where the class begins with 'icon-':
Suitable for font icons, targeting .icon-home, .icon-user, etc.
- Selecting elements where the class ends with '-btn':
Targets .action-btn, .submit-btn, etc.
Use Cases and Limitations
| 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 |
Attribute selectors can be very powerful and flexible, but they have some limitations:
- Performance: Using attribute selectors, especially those with substring matching, can be less performant than class or ID selectors. They require more processing because the browser must evaluate the attribute value for every element against the pattern.
- Specificity: These selectors have the same specificity as an attribute selector, which is less than an ID but more than an element selector. They can be overridden by subsequent declarations that are more specific or important.
These capabilities allow developers to manage CSS rules dynamically and responsively to the nature of the class attributes, promoting a more modular and adaptable styling methodology. Understanding and using these CSS wildcard-like selectors can greatly enhance your ability to efficiently manage styles across a large project.

