title: How is CSS applied by the browser, and are repaints affected by it?
body: <p>Let's say we have an HTML page with a single stylesheet <code>&lt;link&gt;</code>. How does the browser take the rules in this stylesheet and apply it to the HTML? I'm not asking about how to make it faster, I want to know how the rendering itself is handled.</p>
  <p>Does it apply each rule one-by-one as it parses the stylesheet and render the result progressively? Or, are the CSS file's contents completely downloaded, then fully evaluated, and <em>then</em> applied to the HTML all at once? Or something else?</p>
  <p>I ask this after posting an answer earlier on <a href="https://stackoverflow.com/questions/6944190/does-the-order-of-rules-in-a-css-stylesheet-affect-rendering-speed/6945000">a question about CSS rule order affecting rendering speed</a>, with the assumption that the styles were rendered <em>as</em> the stylesheet loaded, so the first rules would be applied before the last ones, and not all at once. I'm not sure where I picked up the idea, it's just something I have always thought.</p>
  <p>I tried a demo on my server that looked like this:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
     &lt;title&gt;Test&lt;/title&gt;
     &lt;link rel=&quot;stylesheet&quot; href=&quot;test.css&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;&lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><code>test.css</code> contents:</p>
  <pre><code>html { background:green }
  /* thousands of lines of irrelevant CSS to make the download slow */
  html { background:red }
  </code></pre>
  <p>Testing in Firefox 5, I expected to see green at first, then turn to red. It didn't happen. I tried with two separate stylesheets with conflicting rules and got the same results. After many combinations, the only way I got it to work was an inline <code>&lt;style&gt;</code> block in the <code>&lt;head&gt;</code>, with the conflicting rules coming from a <code>&lt;link&gt;</code> in the <code>&lt;body&gt;</code> (the body itself was completely empty except for the link tag). Even using an inline <code>style</code> attribute on the <code>&lt;html&gt;</code> tag, and then loading this stylesheet did not create the flicker that I expected.</p>
  <p>Are repaints affected in <em>any</em> way by the CSS, or is the final output applied all at once after the entire stylesheet is downloaded and it's rules computed to what the final output should be? Do CSS files download in paralel with the HTML itself or block it (like script tags do)? How does this actually work?</p>
  <p>I am not looking for optimization tips, I'm looking for authoritative references on the subject, so that I can cite them in the future. It's been very difficult to search for this information without turning up tons of unrelated material. Summary:</p>
  <ul>
  <li>Is all CSS content downloaded before <em>any</em> of it is applied? (reference please)</li>
  <li>How is this affected by things like <code>@import</code>, multiple <code>&lt;link&gt;</code>s, inline style attributes, <code>&lt;style&gt;</code> blocks in the head, and different rendering engines?</li>
  <li>Does the download of CSS content block the downloading of the HTML document itself?</li>
  </ul>