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?
[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".
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With