Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does img[class*="align"] mean in CSS? [duplicate]

What does img[class*="align"] mean in CSS?

I have seen this in many stylesheets, but I'm not sure why it's used and what it does. Any idea?

like image 834
Pallab Kumar Laskar Avatar asked Jun 14 '13 09:06

Pallab Kumar Laskar


People also ask

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".

What does * refers to in CSS?

The * means "all elements" (a universal selector), so we are setting all elements to have zero margins, and zero padding, thus making them look the same in all browsers.

What is a class selector in CSS?

The class selector is a way to select all of the elements with the specified class name, and apply styles to each of the matching elements. The selector must start with a period ( . ) and then the class name.


2 Answers

It will match all img elements that have a class that contains align.

The spec, has more information on this:

W3 Spec on CSS selectors

like image 26
Arran Avatar answered Oct 13 '22 13:10

Arran


It's an attribute selector which matches any img tag class text including "align". For instance, it would match any of the following:

<img class="dummy align test" />
<img class="test align-1" />
<img class="hello-align" />
<img class="abaligncd" />
<img class="align" />

From the documentation (linked above):

E[foo*="bar"] - an E element whose "foo" attribute value contains the substring "bar"

This is used in popular CSS frameworks to style multiple similar classes without having to add a new identical class to each. For instance, if we had the following markup:

<p class="central para-red">Hello, world!</p>
<p class="para-green bold">Hello, world!</p>
<p class="para-blue">Hello, world!</p>
<p>Hello, world!</p>

We could apply styling to all of the p elements whose class contains "para-" without having to manually type all the variations by simply using:

p[class*="para-"] { ... }

Here is a JSFiddle example of this in use.

like image 163
James Donnelly Avatar answered Oct 13 '22 12:10

James Donnelly