Does the order of the rules within a single css file make a difference?
<div id="wrapper>
<div id="a>
section a
</div>
<div id="b>
section b
</div>
<div id="c>
section c
</div>
</div>
Can the div rule for #c need to be below #a and #b, or will the clear:both not work?
#wrapper {
color: #CCC;
}
#c {
clear:both
}
#a {
float: right;
}
#b {
float: left;
}
Order of CSS layouting as defined in the CSS specification: position: absolute may override float: left/right . float: left/right may override display , with the exception of display: none . So the order is: position , float , display .
The !important rule in CSS is used to add more importance to a property/value than normal. In fact, if you use the !important rule, it will override ALL previous styling rules for that specific property on that element!
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.
The more specific the CSS selector is, the higher is the precedence of the CSS property declarations inside the CSS rule owning the selector. In general terms, the more specifically (uniquely) a CSS selector targets an HTML element, the higher is its specificity.
Order is indeed important.
See http://msdn.microsoft.com/en-us/library/aa342531%28VS.85%29.aspx#specf
To quote, in part:
3. Sort by specificity of selector: selectors with higher specificity override selectors that are more general. Specificity is calculated by concatenating the count of (a) ID attributes, (b) class and pseudo-class attributes, and (c) type names and pseudo-elements in the selector.
For example, each of the following selectors could apply to a single LI element; however, only declarations with the highest specificity are applied in the event of a conflict.
* {} /* a=0 b=0 c=0 -> specificity = 0 */
li {} /* a=0 b=0 c=1 -> specificity = 1 */
ul li {} /* a=0 b=0 c=2 -> specificity = 2 */
li:first-line {} /* a=0 b=0 c=2 -> specificity = 2 */
ul ol+li {} /* a=0 b=0 c=3 -> specificity = 3 */
li.class {} /* a=0 b=1 c=1 -> specificity = 11 */
li#id {} /* a=1 b=0 c=1 -> specificity = 101 */
4.Sort by order specified: if two rules have the same weight and specificity, the last one parsed wins. (Note that stylesheets specified with an @import rule are parsed first.) Selectors that appear later in the stylesheet will be used if there is any conflict. For this reason, link pseudo-class selectors should always be used in the following order.
Also see http://www.smashingmagazine.com/2009/08/17/taming-advanced-css-selectors/ for a broader description with more examples.
Note that since there is no overlap in your example, the order is not important. If, on the other hand, you had conflicting styles, then weight, specificity and order would be relevant. I assume that your example in the question is rather simplified.
Order only matters if the rules apply to the same attribute of the same element. (In that case, the last rule "wins".) In your example, the order will make no difference.
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