Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector wildcard inside class name

I would like to know if it's possible to target all classes that starts with col- and ends with 12 only?

For example classes like:

.col-sm-12
.col-md-12
.col-lg-12

but not classes like:

.col-sm-8
.col-lg-6

I tried with something like this which is not working:

[class^="col-*-12"] {
    border: 1px solid red;
}
like image 201
Meek Avatar asked Mar 09 '18 08:03

Meek


People also ask

Can you use wildcard in CSS selector?

Wildcard Selectors (*, ^ and $) in CSS for classesWildcard selector is used to select multiple elements simultaneously. It selects similar type of class name or attribute and use CSS property. * wildcard also known as containing wildcard.

Which selector is used as a wildcard selector?

The universal selector is used as a wildcard character.

What is the purpose of * wildcard in a selection?

A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.


1 Answers

You can use the following solution:

[class^="col-"][class$="-12"] {
    color:red;
}
<span class="col-lg-12">col-lg-12</span>
<span class="col-md-12">col-md-12</span>
<span class="col-lg-8">col-lg-8</span>
<span class="col-md-9">col-md-9</span>

Explanation:

  • [class^="col-"]: The class have to start with col-.
  • [class$="-12"]: The class have to end with -12.

You can find a very good overview of the attribute pattern on CSS tricks.

like image 182
Sebastian Brosch Avatar answered Oct 03 '22 23:10

Sebastian Brosch