346

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; }
fudge
  • 189
  • 1
  • 6
  • 18
jchwebdev
  • 5,034
  • 5
  • 21
  • 30

3 Answers3

669

The following should do the trick:

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

Edit: Added wildcard (*) as suggested by David

Koen.
  • 25,449
  • 7
  • 83
  • 78
  • 63
    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... */}`](http://www.w3.org/TR/selectors/#selectors). – David Thomas Nov 12 '12 at 21:48
  • 38
    even better; to prevent collision `div[class*=' myclass'],div[class^='myclass']` – Koen. Nov 12 '12 at 21:48
  • 1
    agreed, I was about to edit the white-space prefaced version into my comment (when I remembered it), then I saw you'd already added that detail! =) – David Thomas Nov 12 '12 at 21:51
  • 3
    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: http://www.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 Nov 13 '12 at 01:25
  • 3
    @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. – ScottS Nov 13 '12 at 03:11
  • 1
    `*` will include `^`, duplicity. – zrooda May 04 '15 at 15:52
  • 1
    @mystrdat no, notice the space in the `*` version. You don't want to match `x-myclass` by using only the `*` version without a space. – TWiStErRob Jun 16 '15 at 12:29
  • 13
    Not sure why you've overqualified with `div` - or why you didn't include the `-` – sheriffderek Sep 11 '15 at 17:09
  • 1
    Thanks , I used it like this **div:not([class*=myclass-])** in another case – Hamid Shoja Aug 26 '21 at 13:23
  • It would be helpful if you explained what this code is actually doing – Doggit Jun 08 '22 at 10:45
  • for completeness sake, use `div[class$=myclass]` to match for class names ending with `myclass` – ddolce Apr 24 '23 at 18:47
23

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.
Andrew Tibbetts
  • 2,874
  • 3
  • 23
  • 28
James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • 2
    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 – Rowan Nov 12 '12 at 21:51
  • 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. – jchwebdev Nov 13 '12 at 01:28
  • @TWiStErRob Yeah, imagine how I feel - I got -4 and answered FIRST, this guy said the exact same thing AFTER and gets upvotes haha. – Andy Aug 20 '15 at 13:28
  • @Andy - This is not at all the same as your answer, unless it has been changed. It's almost the opposite actually. – sheriffderek Sep 11 '15 at 17:05
  • @TWiStErRob - while this is good practice, - using a block modifier approach, it in no way answers the question. – sheriffderek Sep 11 '15 at 17:06
  • @sheriffderek why is it the "opposite"? Both have general classes for base and special classes for overrides. I agree that it's not a direct answer (the magic attribute matching I'm not too fond of), but I don't see any logical difference between the two answers by Andy and James, only their rep level. – TWiStErRob Sep 11 '15 at 17:18
  • First off, they are `divs` - ok, that's in common... then in one answer there is a `myclass` - which says "all divs with myclass" - and then there is a modifier class of `class-1, class-2, class-3 etc... ` So - divs are square, myclass is round, class-1 is red .. this builds up 'cascading' styles as was intended. Your answer, @TWiStErRob - is too specific, too early - and is much less modular an reusable. It's basically backwards. – sheriffderek Sep 11 '15 at 18:06
  • 1
    @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.) – TWiStErRob Sep 11 '15 at 18:23
  • I really can't understand why I got another downvote on this almost 3 years later (or why Andy is -5 for that matter). I still stand by this answer, multiple classes is a much cleaner approach than partial matching a class attribute. Sure there are reasons you might have to and Koen's answer answers that, but I guarentee plenty of people who found this question don't have a good excuse for not using multple classes. And for the record, I'm sure my rep was considerably lower 3 years ago. – James Montagne Sep 11 '15 at 21:47
  • @sheriffderek can you explain why it's the OPPOSITE (lol I read that almost fell off my chair, one of us has to be drunk to not understand they are the same). What am I missing? And JamesMontagne you are correct, I still think it's the best solution too, hence why I stated the exact same thing you said, when I saw no other answers yet, however long ago this question was. – Andy Sep 16 '15 at 19:03
  • A square is a rectangle - but a rectangle is not a square. – sheriffderek Sep 16 '15 at 21:31
  • They have the same outcome... http://codepen.io/sheriffderek/pen/YyqybM?editors=110 – sheriffderek Sep 16 '15 at 21:53
  • `class="x a", class="x b", class="x c"` @Andy These answers are literally identical. – Wylliam Judd Nov 01 '17 at 21:19
  • 1
    @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! – Andy Nov 03 '17 at 12:59
4

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 {...}
TMB
  • 4,683
  • 4
  • 25
  • 44
Andy
  • 1,123
  • 6
  • 9
  • I don't think this answer is *that* bad. I would just edit another-class to be myclass... class="myclass myclass-one" – TMB Oct 12 '15 at 18:40
  • now its similar to the above – TMB Oct 12 '15 at 18:42
  • I did not see that thread above... – TMB Oct 12 '15 at 18:46
  • 2
    I still wouldn't give either of these answers too many points, because they don't answer the wildcard question. – TMB Oct 12 '15 at 18:54
  • 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. – Andy Oct 19 '15 at 20:02