Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the order of rules within a single css file important?

Tags:

css

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;
}
like image 454
Jeff Avatar asked Oct 21 '09 14:10

Jeff


People also ask

What is the correct order of the CSS rule?

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 .

What is the important rule in CSS?

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!

In what order are CSS classes 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.

What gives the highest precedence to a CSS selector?

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.


2 Answers

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.

like image 150
Jonathan Fingland Avatar answered Oct 26 '22 12:10

Jonathan Fingland


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.

like image 23
Martha Avatar answered Oct 26 '22 11:10

Martha