class overrule when two classes assigned to one div

ClassCssHtml

Class Problem Overview


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?

Class Solutions


Solution 1 - Class

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.

Solution 2 - Class

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; }

blue red
red blue

vs

blue last in css

.red { color: red; } .blue{ color: blue; }

blue red
red blue

Solution 3 - Class

If you asking about they have same property then as per the CSS rule it's take the last statement.

<div class="red green"></div>

CSS

.red{
 color:red;
}
.green{
 color:green;
}

As per the above example it's take the last statement as per css tree which is .green.

Solution 4 - Class

The class that is defined last in the CSS have priority, if nothing else applies.

Read up on CSS priority to see how it works.

Solution 5 - Class

Many classes can be assigned to an element, you just separate them with a space

<div class="myClass aSecondClass keepOnClassing stayClassySanDiego"></div>

Because of the cascade in CSS, the overwriting rules closest the to bottom of the document will be applied to the element.

So if you have

.myClass
{
    background: white;
    color: blue;
}

.keepOnClassing
{
    color: red;
}

The red color will be used, but not the background color as it was not overwritten.

You must also take into account CSS specificity, if you have a more specific selector, this one will be used:

.myClass
{
    background: white;
    color: blue;
}
    
div.myClass.keepOnClassing
{
    background: purple;
    color: red;
}

.stayClassySanDiego
{
    background: black;
}

The second selector here will be used as it is more specific.

You can take a look at it all here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel Ramirez-EscuderoView Question on Stackoverflow
Solution 1 - Classjfriend00View Answer on Stackoverflow
Solution 2 - ClassErez SasonView Answer on Stackoverflow
Solution 3 - ClasssandeepView Answer on Stackoverflow
Solution 4 - ClassHenrik AmmerView Answer on Stackoverflow
Solution 5 - ClassKyleView Answer on Stackoverflow