28

I want to target all of the columns inside a Bootstrap container so I can give then a similar style. For example:

<div class="container unique">
  <div class="row">
    <div class="col-md-3"></div>
    <div class="col-md-3"></div>
    <div class="col-md-3"></div>
    <div class="col-md-3"></div>
  </div>
</div>

I can target this with CSS:

.unique .col-md-3{...}

but what I want to do is when I have many different col-* elements, to target them all together.

I tried this:

.unique .col-*{...}

but it didn't work. Can I do this with CSS?

alex
  • 5,636
  • 9
  • 47
  • 97

2 Answers2

60

Pedro, what you're looking for is called attribute selector. In your particular case, you can use it like this:

.unique [class~=col] {color:red }

but you could also use this with more wide options like

[class*='col-']

to cover preceding white spaces.

Also, here you have the same documentation in Spanish

Haifeng Zhang
  • 27,283
  • 17
  • 68
  • 115
Devin
  • 7,573
  • 6
  • 37
  • 53
19

The CSS attribute contains selector can be used to achieve this:

.unique [class*=col]{...}

MDN is a useful reference site for CSS selectors. For reference, the attribute selectors are found here.

Rich O'Kelly
  • 40,274
  • 9
  • 81
  • 111