Match All Class Selectors That Begin with? [duplic

2019-01-07 04:24发布

This question already has an answer here:

Is it possible to use a "wildcard" for selectors in CSS3?

eg.

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

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

.myclass* { color: #f00; }

Possible?

标签: css match
3条回答
Anthone
2楼-- · 2019-01-07 04:37

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.
查看更多
3楼-- · 2019-01-07 04:43

The following should do the trick:

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

Edit: Added wildcard (*) as suggested by David

查看更多
欢心
4楼-- · 2019-01-07 04:44

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 {...}
查看更多
登录 后发表回答