Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple classes in one element and specificity

Tags:

Just wondering when you use multiple classes on the one element such as class="foo bar" and those classes are setup as below:

.foo {     margin-right: 10px; }   .bar {     margin-right: 0px; } 

Which class will have specificity? Will the margin be 10px or 0px?

like image 763
Brett Avatar asked Dec 09 '11 20:12

Brett


People also ask

Can you have multiple classes on an element?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.

How do I use multiple CSS classes on a single element?

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.

Can you put multiple classes in a div?

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well. All you have to do is separate each class with a space.


1 Answers

It works based on precedence within the CSS. Therefore the item to occur most recently will override any previous styles.

CASE 1

.foo  { background : red; } .bar  { background : blue; } 

class = 'foo bar' would be blue in this instance.

CASE 2

.bar  { background : blue; } .foo  { background : red; }  

class = 'foo bar' would be red in this instance.

Working Example

like image 184
Rion Williams Avatar answered Sep 20 '22 00:09

Rion Williams