395

Is it possible to use a "wildcard" for elements having a class name starting with a specific string in CSS3?

Example:

<div class="myclass-one"></div>
<div class="myclass-two"></div>
<div class="myclass-three"></div>

and then magically set all the above divs to red in one go:

.myclass* { color: #f00; }
2
  • 11
    [class^='myclass-'], [class*=' myclass-'] { color: red } Commented May 26, 2021 at 11:59
  • 17
    [^="start"] [*="contain"] [$="ends"] ( ^ * $ rules are from Regular expressions ) Commented Nov 15, 2022 at 15:14

3 Answers 3

753

The following should do the trick:

div[class^='myclass'], div[class*=' myclass']{
    color: #F00;
}

Edit: Added wildcard (*) as suggested by David

Sign up to request clarification or add additional context in comments.

11 Comments

Only if the class beginning with myclass is also the first class-name contained within the class attribute. You might want to try div[class*=myclass] { /* ...stuff... */}.
even better; to prevent collision div[class*=' myclass'],div[class^='myclass']
Schweet! I always forget about the attribute thing (probably because it looks so ugly.) What I -still- don't understand is this: According to the W3C page: w3schools.com/cssref/css_selectors.asp the syntax is: div[class*="test"] Yet, it -seems- to work like this, with no 'element' specified: [class*="test"] ...which I don't get. IOW, -why- does this work? (I'm not complaining, I like it -better-!) I just don't want to start using it and find out that it's a bug that later gets 'fixed'. Any ideas? THANKS! ---JC
@jchwebdev--it is not a bug. The attribute selector is just like any other selector (it can be used as a "stand alone"). It is no different that .myClass potentially being used with div.myClass instead. The attribute selector can stand alone (matching any elements that meet that criteria) or be narrowed to specific element. So don't worry about it being odd to have [class*="myClass"] all by itself.
Not sure why you've overqualified with div - or why you didn't include the -
|
26

It's not a direct answer to the question, however I would suggest in most cases to simply set multiple classes to each element:

<div class="myclass one"></div>
<div class="myclass two"></div>
<div class="myclass three"></div>

In this way you can set rules for all myclass elements and then more specific rules for one, two and three.

.myclass { color: #f00; }

.two { font-weight: bold; }

etc.

15 Comments

By using multiple classes rather than looking for prefixes, you also save yourself a few battles with specificity and selector speed. Attribute selectors are slow, but also are really very specific compared to just classes. +1
I get it. I'm using WordPress, which assigns similar named classes based on 'slug'. So the attribute solution is -way- easier than trying to add additional classes to each relevant page.
@TWiStErRob Yeah, imagine how I feel - I got -4 and answered FIRST, this guy said the exact same thing AFTER and gets upvotes haha.
@sheriffderek I still don't see it. There's a 1:1 correspondence: myclass<->another-class, one<->myclass-one also you may need to be specific with .generic.special to increase the selector's specificity to apply overrides for color for example. (Not my answer, I just don't see the difference, and the votes feel unfair. Also the div comes from the question.)
@WylliamJudd - Correct. Which is why the whole comments took off to begin with. The answers are identical, I submitted my answer first but was downvoted (originally) and this one was upvoted - hence all the confusion. But oh well as long as they help people in the future solve similar issues - great!
|
5

You can easily add multiple classes to divs... So:

<div class="myclass myclass-one"></div>
<div class="myclass myclass-two"></div>
<div class="myclass myclass-three"></div>

Then in the CSS call to the share class to apply the same styles:

.myclass {...}

And you can still use your other classes like this:

.myclass-three {...}

Or if you want to be more specific in the CSS like this:

.myclass.myclass-three {...}

5 Comments

I don't think this answer is that bad. I would just edit another-class to be myclass... class="myclass myclass-one"
now its similar to the above
I did not see that thread above...
I still wouldn't give either of these answers too many points, because they don't answer the wildcard question.
Correct, it is similar to the one above, ironically it was posted first & got negative votes. Haha. Also, it's not a DIRECT answer, but an alternate solution that perhaps the original author didn't think about & probably the ideal solution in all scenarios in which you have full control over HTML & CSS.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.