CSS
Web Development
Programming
Coding Techniques
HTML Classes

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

  1. Attribute presence and value selectors:
    • [attr]: elements that have the attribute, whatever its value.
    • [attr="value"]: elements whose attribute equals the value.
  2. 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".
  3. 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".
  4. Start of a dash-separated value:
    • [attr|="value"]: mainly used for matching language codes and subcodes.

Examples with class names

  • Class containing btn:
css
  [class*="btn"] {
    background-color: yellow;
  }

This matches .btn, .my-btn and any other class containing the letters btn.

  • Class beginning with icon-:
css
  [class^="icon-"] {
    font-family: "Icons";
  }

Handy for font icons such as .icon-home and .icon-user.

  • Class ending with -btn:
css
  [class$="-btn"] {
    padding: 10px;
  }

Matches .action-btn, .submit-btn and so on.

Quick reference

Use CaseSelectorDescriptionExample
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&#126;="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 !important declarations can override them.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms