29

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
  • 6,818
  • 9
  • 52
  • 103

2 Answers2

61

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
  • 30,077
  • 19
  • 81
  • 125
Devin
  • 7,690
  • 6
  • 39
  • 54
  • 3
    The first selector won't match the columns, the second one may or may not. http://jsbin.com/rajaw/1/edit – Hashem Qolami Sep 21 '14 at 06:01
  • 6
    According to the linked documentation, the 100% correct form that matches the pattern in question is `.unique [class^=col-] {color: red}` – Attila Fulop Apr 04 '16 at 12:22
  • Keep in mind, you select all classes which begin with col-. That includes col-md-offset-... – aProgger Jan 22 '17 at 12:58
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
  • 41,274
  • 9
  • 83
  • 114