{"id":123,"date":"2006-10-26T11:45:11","date_gmt":"2006-10-26T11:45:11","guid":{"rendered":"http:\/\/lachy.id.au\/log\/2006\/10\/cascade2"},"modified":"2006-12-14T00:02:17","modified_gmt":"2006-12-14T00:02:17","slug":"cascade2","status":"publish","type":"post","link":"https:\/\/lachy.id.au\/log\/2006\/10\/cascade2","title":{"rendered":"The Cascade: Part 2 (Finally!)"},"content":{"rendered":"<p>Exactly one year ago from this day, I published <a href=\"\/log\/2005\/10\/cascade1\" title=\"The Cascade: Part 1\">part<br \/>\n\t\t1<\/a> in a series of articles about<br \/>\n\tCSS cascading and inheritance.  However, due to various factors (mostly laziness),<br \/>\n\tthe sequel never got published\u2026 Until now!  Today, I\u2019m going to take a break<br \/>\n\t\tfrom the XBL series of articles (which <em>will<\/em> resume in a day or so) and<br \/>\n\t\tfinally publish the long awaited conclusion to this series. If you haven\u2019t<br \/>\n\t\tread part 1 (or even if did a long time ago), I suggest you do so now<br \/>\n\t\tbefore continuing.<\/p>\n<p>Following on from the first article in which we looked at how to find all<br \/>\n\tthe style declarations that applied to each element, we\u2019re going to show how<br \/>\n\tthese are sorted by order of precedence, to determine which ones are applied<br \/>\n\tto the element.<\/p>\n<h3 id=\"cascade2-sorting\">Sorting<\/h3>\n<p>Steps 2, 3 and 4 of the algorithm deal with sorting the declarations into<br \/>\n\tthe order of precedence.  From the exercise in the part 1, we were left with<br \/>\n\t4 rule sets which applied to the p element in the sample document.  For the<br \/>\n\tpurpose of this exercise, I\u2019m just going to add a few more declarations and<br \/>\n\tannotate them with their origin.<\/p>\n<pre><code>p { margin: 1em 0; } \/* User Agent *\/\r\n* { background: blue none !important; color: white !important; } \/* User *\/\r\nP { text-indent: 1em; text-align: left; } \/* User *\/\r\np { text-align: justify; } \/* User *\/\r\n#content p { margin: .8em 0; line-height: 1.2; text-indent: 0 !important; } \/* Author *\/\r\np { line-height: 1.4; } \/* Author *\/<\/code><\/pre>\n<h4 id=\"cascade2-step2\">Step 2<\/h4>\n<p>Step 2 of the cascade says to sort the rules according to importance and origin.<br \/>\n\tThe following is <a href=\"http:\/\/www.w3.org\/TR\/CSS21\/cascade.html#cascading-order\">the<br \/>\n\torder of precedence specified in CSS 2.1<\/a>.<\/p>\n<ol>\n<li>user agent declarations<\/li>\n<li>user normal declarations<\/li>\n<li>author normal declarations<\/li>\n<li>author important declarations<\/li>\n<li>user important declarations<\/li>\n<\/ol>\n<p>If we discard the selectors for now (they\u2019ll be need again in <a href=\"#cascade2-step3\">step<br \/>\n\t\t3<\/a> below), we\u2019re<br \/>\n\tleft with a list of declarations.  A declaration is a property and its associated<br \/>\n\tvalue.  We can then proceed to sort them into the order specified.<\/p>\n<ol>\n<li id=\"cascade2-uadecl\">User Agent Declarations\n<ol>\n<li><code>margin: 1em 0;<\/code><\/li>\n<\/ol>\n<\/li>\n<li id=\"cascade2-usernormdecl\">User Normal Declarations\n<ol>\n<li><code>text-indent: 1em;<\/code><\/li>\n<li><code>text-align: left;<\/code><\/li>\n<li><code>text-align: justify;<\/code><\/li>\n<\/ol>\n<\/li>\n<li id=\"cascade2-authornormdecl\">Author Normal Declarations\n<ol>\n<li><code>margin: .8em 0;<\/code><\/li>\n<li><code>line-height: 1.2;<\/code><\/li>\n<li><code>line-height: 1.4;<\/code><\/li>\n<\/ol>\n<\/li>\n<li id=\"cascade2-authorimpdecl\">Author Important Declarations\n<ol>\n<li><code>text-indent: 0 !important;<\/code><\/li>\n<\/ol>\n<\/li>\n<li id=\"cascade2-userimpdecl\">User Important Declarations\n<ol>\n<li><code>background: blue none !important;<\/code><\/li>\n<li><code>color: white !important;<\/code><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h4 id=\"cascade2-step3\">Step 3<\/h4>\n<p>Step 3 of the cascade then says to sort the rules with the same importance<br \/>\n\tand origin by the specificity of the selector.  The details of <a href=\"http:\/\/www.w3.org\/TR\/CSS21\/cascade.html#specificity\">calculating<br \/>\n\tthe specificity<\/a> is out of scope for this article, but it has been covered by others.<br \/>\n\tSee Andy Clarke\u2019s <a href=\"http:\/\/www.stuffandnonsense.co.uk\/archives\/css_specificity_wars.html\">Specificity<br \/>\n\tWars<\/a> and Molly Holzschlag\u2019s <a href=\"http:\/\/www.molly.com\/2005\/10\/06\/css2-and-css21-specificity-clarified\/\">CSS2<br \/>\n\tand CSS2.1 Specificity Clarified<\/a>.<\/p>\n<p>This step is important for cases where two declarations for the same property<br \/>\n\thave the same importance and origin.  In this example, this occurs for both<br \/>\n\t<a href=\"#cascade2-usernormdecl\">user normal declarations<\/a> (two text-align declarations) and <a href=\"#cascade2-authornormdecl\">author<br \/>\n\tnormal declarations<\/a>\t(two line-height declarations).<\/p>\n<p>For the <a href=\"#cascade2-usernormdecl\">user normal declarations<\/a>, these are the rule sets involved:<\/p>\n<pre><code>p { text-indent: 1em; text-align: left; } \/* User *\/\r\np { text-align: justify; } \/* User *\/<\/code><\/pre>\n<p>As you can see, both have the same selector (<code>p<\/code>), which has a specificy of<br \/>\n\t0,0,1.  So, sorting by specificity in this case makes no difference.  However,<br \/>\n\tfor the <a href=\"#cascade2-authornormdecl\">author normal declarations<\/a>, these are the rule sets involved:<\/p>\n<pre><code>p { line-height: 1.4; } \/* Author *\/\r\n#content p { margin: .8em 0; line-height: 1.2; text-indent: 0 !important; } \/* Author *\/<\/code><\/pre>\n<p>These 2 rule sets each use different selectors which have different specificity.<br \/>\n\tThe selector #content p has a specificity of 101.  The selector p has a specificity<br \/>\n\tof 0,0,1.  Since 1,0,1 is a higher specificity than 0,0,1, the former takes<br \/>\n\tprecedence.  So the order of <a href=\"#cascade2-authornormdecl\">author normal<br \/>\n\tdeclarations<\/a> is changed to the following:<\/p>\n<ol>\n<li><code>margin: .8em 0;<\/code><\/li>\n<li><code>line-height: 1.4;<\/code><\/li>\n<li><code>line-height: 1.2;<\/code><\/li>\n<\/ol>\n<h4 id=\"cascade2-step4\">Step 4<\/h4>\n<p>The forth and final step of the sorting process involves sorting declarations<br \/>\n\twhich have the same importance, origin and specificity by the order they are<br \/>\n\tspecified in the CSS.  This is where the order of the <a href=\"#cascade2-usernormdecl\">user<br \/>\n\tnormal declarations<\/a>\tfrom <a href=\"#cascade2-step3\">step 3<\/a> is resolved.  In this example, given that I listed the declarations<br \/>\n\tin the order in which they appeared, no change needs to be made to the above<br \/>\n\tlist.<\/p>\n<p>In cases where there is more than one declaration for a property, the latter<br \/>\n\tdeclaration overwrites the former, which is effectively discarded.  This leaves<br \/>\n\tthe following list of declarations to be applied to element.<\/p>\n<ol>\n<li><code>text-align: justify;<\/code><\/li>\n<li><code>margin: .8em 0;<\/code><\/li>\n<li><code>line-height: 1.4;<\/code><\/li>\n<li><code>text-indent: 0;<\/code><\/li>\n<li><code>background: blue none;<\/code><\/li>\n<li><code>color: white;<\/code><\/li>\n<\/ol>\n<p>This concludes the series about the cascade, but the related issue of inheritence<br \/>\n\tstill needs to be addressed and I intend to do so at some point in the future.<br \/>\n   However, I don&#8217;t expect that it will take another year before I do\u2026 But who<br \/>\n\tknows? \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The long awaited conclusion to my previous article, from last year: The Cascade: Part 1!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3],"tags":[],"_links":{"self":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":0,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}