Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class overrule when two classes assigned to one div

Tags:

html

css

class

I was creating a <div> tag in which I wanted to apply two classes for a <div> tag which would be a thumbnail gallery. One class for its position and the other class for its style. This way I could apply the style, I was having some strange results which brought me to a question.

Can two classes be assigned to a <div> tag? If so, which one overrules the other one or which one has priority?

like image 866
Daniel Ramirez-Escudero Avatar asked Sep 04 '12 07:09

Daniel Ramirez-Escudero


People also ask

Can 1 div have 2 classes?

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.

Can you use two classes one element?

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


2 Answers

Multiple classes can be assigned to a div. Just separate them in the class name with spaces like this:

<div class="rule1 rule2 rule3">Content</div> 

This div will then match any style rules for three different class selectors: .rule1, .rule2 and .rule3.

CSS rules are applied to objects in the page that match their selectors in the order they are encountered in the style sheet and if there is a conflict between two rules (more than one rule trying to set the same attribute), then CSS specificity determines which rule takes precedence.

If the CSS specificity is the same for the conflicting rules, then the later one (the one defined later in the stylesheet or in the later stylesheet) takes precedence. The order of the class names on the object itself does not matter. It is the order of the style rules in the style sheet that matters if the CSS specificity is the same.

So, if you had styles like this:

.rule1 {     background-color: green; }  .rule2 {     background-color: red; } 

Then, since both rules match the div and have exactly the same CSS specificity, then the second rule comes later so it would have precedence and the background would be red.


If one rule had a higher CSS specificity (div.rule1 scores higher than .rule2):

div.rule1 {     background-color: green; }  .rule2 {     background-color: red; } 

Then, it would take precedence and the background color here would be green.


If the two rules don't conflict:

.rule1 {     background-color: green; }  .rule2 {     margin-top: 50px; } 

Then, both rules will be applied.

like image 170
jfriend00 Avatar answered Sep 21 '22 16:09

jfriend00


Actually, the class that defined last in the css - is applied on your div.

check it out:

red last in css

.blue{ color: blue; }  .red { color: red;  }
<div class="blue red">blue red</div>  <div class="red blue">red blue</div>

vs

blue last in css

.red { color: red;  }  .blue{ color: blue; }
<div class="blue red">blue red</div>  <div class="red blue">red blue</div>
like image 40
Erez Sason Avatar answered Sep 21 '22 16:09

Erez Sason