Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match all elements having class name starting with a specific string [duplicate]

Tags:

css

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; }
like image 974
jchwebdev Avatar asked Oct 20 '22 11:10

jchwebdev


2 Answers

The following should do the trick:

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

Edit: Added wildcard (*) as suggested by David

like image 168
Koen. Avatar answered Oct 27 '22 09:10

Koen.


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.
like image 42
James Montagne Avatar answered Oct 27 '22 11:10

James Montagne