Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 635
Suzi Larsen Avatar asked Mar 19 '14 14:03

Suzi Larsen


People also ask

How do you select a col in CSS?

You could use [class*="col-"] CSS attribute selector to select any element contains at least one occurrence of col- as its class value. If all values of class attributes begin with col- , you could use [class^="col-"] selector. WORKING DEMO. To avoid styling unintended classes like .

Can you give element multiple classes CSS?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Which selector is used to apply CSS to a group of elements?

The CSS class Selector The class selector selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name.


2 Answers

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.

like image 89
Hashem Qolami Avatar answered Oct 19 '22 05:10

Hashem Qolami


You can use

CSS [attribute*=value] selector

<style> div[class*=col-]{ color: green; } </style>  <body> <div class="col-4"> <p>this is an example one</p> <div> <div class="test"> <p>this is an example one</p> <div> <div class="col-8"> <p>this is an example three</p> <div> </body> 

Please check below link

https://www.w3schools.com/cssref/sel_attr_contain.asp

https://www.w3schools.com/cssref/tryit.asp?filename=trycss3_attr_contain

like image 36
Arshad Syed Avatar answered Oct 19 '22 03:10

Arshad Syed