Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wildcard * in CSS for classes

I have these divs that I'm styling with .tocolor, but I also need the unique identifier 1,2,3,4 etc. so I'm adding that it as another class tocolor-1.

<div class="tocolor tocolor-1">   tocolor 1   </div> <div class="tocolor tocolor-2">   tocolor 2   </div> <div class="tocolor tocolor-3">   tocolor 3   </div> <div class="tocolor tocolor-4">   tocolor 4   </div>  .tocolor{   background: red; } 

Is there a way to have just 1 class tocolor-*. I tried using a wildcard * as in this css, but it didn't work.

.tocolor-*{   background: red; } 
like image 207
twitter Avatar asked Feb 24 '11 20:02

twitter


People also ask

What is the purpose of * In wildcard selector?

Mainly there are 2 wildcards: 1. Asterisk (*): It is used for replacing 1 or more characters from a selector attribute.

What is * used for in CSS?

The asterisk (*) is known as the CSS universal selectors. It can be used to select any and all types of elements in an HTML page. The asterisk can also be followed by a selector while using to select a child object. This selector is useful when we want to select all the elements on the page.

What does class *= mean in CSS?

[class*="button_type"] is CSS class Selector (equivalent to CSS attribute selector) means that will select all elements whose class contains at least one substring "button_type".

How do I select a class in CSS?

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


2 Answers

What you need is called attribute selector. An example, using your html structure, is the following:

div[class^="tocolor-"], div[class*=" tocolor-"] {     color:red  } 

In the place of div you can add any element or remove it altogether, and in the place of class you can add any attribute of the specified element.

[class^="tocolor-"] — starts with "tocolor-".
[class*=" tocolor-"] — contains the substring "tocolor-" occurring directly after a space character.

Demo: http://jsfiddle.net/K3693/1/

More information on CSS attribute selectors, you can find here and here. And from MDN Docs MDN Docs

like image 133
Sotiris Avatar answered Oct 11 '22 11:10

Sotiris


Yes you can do this.

*[id^='term-']{     [css here] } 

This will select all ids that start with 'term-'.

As for the reason for not doing this, I see where it would be preferable to select this way; as for style, I wouldn't do it myself, but it's possible.

like image 43
thomas.han Avatar answered Oct 11 '22 11:10

thomas.han