I know CSS selector with the highest specificity takes precedence (i.e. .classname
< #idname
).
I also know that if things are the same specificity, then the last statement called takes precedence:
.classname1 { color: red; } .classname1 { color: blue; } // classname1 color will be blue
Does the ordering of HTML classes on a DOM element affect the statement priority?
CSS Order Matters In CSS, the order in which we specify our rules matters. If a rule from the same style sheet, with the same level of specificity exists, the rule that is declared last in the CSS document will be the one that is applied.
The order in which the attributes are overwritten is determined by where they appear in the CSS, not by the order the classes are defined in the class attribute, i.e. the styling which is defined at the end of the CSS class will be the one to be applied.
Inline styles. Internal (embedded) styles. External styles.
Inline style: Inline style has highest priority. Identifiers(ID): ID have the second highest priority. Classes, pseudo-classes and attributes: Classes, pseudo-classes and attributes are come next. Elements and pseudo-elements: Elements and pseudo-elements have lowest priority.
I have to disagree slightly with Jon and Watson's answers, as...
Your question is:
Does the ordering of CSS classes on a DOM element affect the statement priority?
Which does depend on the statement in question.
HTML Ordering Does Not Typically Matter
The following are equivalent when it comes to a straight call to a class (i.e. .class1
or .class2
) or to a combined call (i.e. .class1.class2
or .class2.class1
):
<div class="class1 class2"></div> <div class="class2 class1"></div>
Cases Where Statement Priority for above HTML Can be Affected Based on HTML Order
The main place where ordering in HTML matters is with the use of attribute selectors in your CSS.
Example 1 Fiddle using the following code seeking to match attribute value will NOT have any change in font color, and each div
will have different properties because of the ordering of the classes:
[class="class1"] { color: red; } [class="class1 class2"] { background-color: yellow; } [class="class2 class1"] { border: 1px solid blue; }
Example 2 Fiddle using the following code seeking to match beginning of attribute value will NOT have any change in font color for the second div
, and each div
will have different properties because of the ordering of the classes:
[class^="class1"] { color: red; } [class^="class1 class2"] { background-color: yellow; } [class^="class2 class1"] { border: 1px solid blue; }
Example 3 Fiddle using the following code seeking to match end of attribute value will NOT have any change in font color for the first div
, and each div
will have different properties because of the ordering of the classes:
[class$="class1"] { color: red; } [class$="class1 class2"] { background-color: yellow; } [class$="class2 class1"] { border: 1px solid blue; }
To be clear, in the cases above, what is affected as far as "statement priority" is concerned is really a matter of whether the statement actually applies or not to the element. But since the application or not is in a sense, the basic priority, and since the above are cases where such application is actually based on the ordering of the classes on the HTML Dom element (rather than the presence or absence of the class), I thought it worth adding this as an answer.
This is a thought occurring to me, based on BoltClock's comment. Consider just two classes being used to style elements based on whatever factors are deemed critical to different styling. These two classes theoretically can replace the use of eleven different individual classes using the combination of attribute selectors (actually, as will be noted later, the possibilities are almost limitless with but a single class, but I'll discuss that in a moment since this post is about ordering of multiple classes). For these two classes we can style elements differently as follows:
Assuming these HTML Combinations
<div class="class1">Element 1</div> <div class="class2">Element 2</div> <div class="class1 class2">Element 3</div> <div class="class2 class1">Element 4</div>
CSS Possibilities
/* simply has the class */ .class1 {} /* affects elements 1, 3, 4 */ .class2 {} /* affects elements 2-4 */ /* has only a single class */ [class="class1"] {} /* affects element 1 only */ [class="class2"] {} /* affects element 2 only */ /* simply has both classes */ .class1.class2 {} /* affects elements 3-4 */ /* has both classes, but in a particular order */ [class="class1 class2"] {} /* affects element 3 only */ [class="class2 class1"] {} /* affects element 4 only */ /* begins with a class */ [class^="class1"] {} /* affects elements 1 & 3 only */ [class^="class2"] {} /* affects elements 2 & 4 only */ /* ends with a class NOTE: that with only two classes, this is the reverse of the above and is somewhat superfluous; however, if a third class is introduced, then the beginning and ending class combinations become more relevant. */ [class$="class1"] {} /* affects elements 2 & 4 only */ [class$="class2"] {} /* affects elements 1 & 3 only */
If I calculate right, 3 classes could give at least 40 combinations of selector options.
To clarify my note about "limitless" possibilities, given the right logic, a single class can potentially have imbedded in it code combinations that are looked for via the [attr*=value]
syntax.
Is all this too complex to manage? Possibly. That may depend on the logic of exactly how it is implemented. The point I am trying to bring out is that it is possible with CSS3 to have ordering of classes be significant if one desired it and planned for it, and it might not be horribly wrong to be utilizing the power of CSS in that way.
No, it does not. The relevant part of the W3C standard makes no mention of the order of appearance for classes.
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