When assigning multiple classes to a div, you can separate them by spaces within the class attribute like this:
<div class="class1 class2 class3">Content</div>
The div will then apply style rules for each of the three different class selectors: .class1
, .class2
, and .class3
.
CSS rules are executed on elements that match their selectors in the sequence they appear in the style sheet. In case of a conflict between two rules trying to define the same attribute, CSS specificity determines which rule takes precedence.
If conflicting rules have the same CSS specificity, the one defined later in the stylesheet or in a subsequent stylesheet will prevail. The order of class names on the element itself doesn't affect this - it's the order of style rules in the stylesheet that matters in such cases.
For example, with styles like:
.class1 {
background-color: green;
}
.class2 {
background-color: red;
}
Both rules match the div and have equal CSS specificity, so the second rule would take precedence, resulting in a red background.
If one rule has higher CSS specificity (div.class1
ranking higher than .class2
):
div.class1 {
background-color: green;
}
.class2 {
background-color: red;
}
The higher specificity rule will take precedence, making the background color green in this case.
If the two rules do not conflict:
.class1 {
background-color: green;
}
.class2 {
margin-top: 50px;
}
Both rules will be applied without any issues.