Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

css selectors - Apply CSS to a group of css classes containing col- substring

I wasn't sure of the best way to word this and that's probably why I have struggled to research it even though it is probably simple.

What I want to do is a apply CSS to a group of classes.

For example I have the classes .col-1, .col-2, .col-3, .col-4

What I want to be able to do is make some css that can say maybe change the border color for all classes with the text col- so I don't have to apply it to each individual number.

I'm sure I've seen this before but cannot think how to do it.

question from:https://stackoverflow.com/questions/22509402/apply-css-to-a-group-of-css-classes-containing-col-substring

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use [class*="col-"] CSS attribute selector to select any element contains at least one occurrence of col- as its class value.

[class*="col-"] {
    border-color: red;
}

If all values of class attributes begin with col-, you could use [class^="col-"] selector.

However, in order to prevent for classes like foo-col-1 to be selected, you could use a combination of two above selectors as follows (Thanks to @JosephSpens):

[class^="col-"], [class*=" col-"] {
  border-color: red;
}

WORKING DEMO.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...