Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6219269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:45:00+00:00 2026-05-24T07:45:00+00:00

Let’s say we have an HTML page with a single stylesheet <link> . How

  • 0

Let’s say we have an HTML page with a single stylesheet <link>. 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.

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 then applied to the HTML all at once? Or something else?

I ask this after posting an answer earlier on a question about CSS rule order affecting rendering speed, with the assumption that the styles were rendered as 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.

I tried a demo on my server that looked like this:

<!DOCTYPE html>
<html>
<head>
   <title>Test</title>
   <link rel="stylesheet" href="test.css" />
</head>
<body></body>
</html>

test.css contents:

html { background:green }
/* thousands of lines of irrelevant CSS to make the download slow */
html { background:red }

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 <style> block in the <head>, with the conflicting rules coming from a <link> in the <body> (the body itself was completely empty except for the link tag). Even using an inline style attribute on the <html> tag, and then loading this stylesheet did not create the flicker that I expected.

Are repaints affected in any 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?

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:

  • Is all CSS content downloaded before any of it is applied? (reference please)
  • How is this affected by things like @import, multiple <link>s, inline style attributes, <style> blocks in the head, and different rendering engines?
  • Does the download of CSS content block the downloading of the HTML document itself?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:45:01+00:00Added an answer on May 24, 2026 at 7:45 am

    How does the browser take the rules in this stylesheet and apply it to the HTML?

    Typically this is done in a streaming fashion. The browser reads the HTML tags as a stream, and applies what rules it can to the elements it has seen so far. (Obviously this is a simplification.)

    An interesting related Q&A: Use CSS selectors to collect HTML elements from a streaming parser (e.g. SAX stream) (a diversion while I search for the article I have in mind).


    Ah, here it is: Why we don’t have a parent selector.

    We often think of our pages as these full and complete documents full of elements and content. However, browsers are designed to handle documents like a stream. They begin to receive the document from the server and can render the document before it has completely downloaded. Each node is evaluated and rendered to the viewport as it is received.

    Take a look at the body of an example document:

    <body>
       <div id="content">
          <div class="module intro">
             <p>Lorem Ipsum</p>
          </div>
          <div class="module">
             <p>Lorem Ipsum</p>
             <p>Lorem Ipsum</p>
             <p>Lorem Ipsum <span>Test</span></p>
          </div>
       </div>
    </body>
    

    The browser starts at the top and sees a body element. At this point,
    it thinks it’s empty. It hasn’t evaluated anything else. The browser
    will determine what the computed styles are and apply them to the
    element. What is the font, the color, the line height? After it
    figures this out, it paints it to the screen.

    Next, it sees a div element with an ID of content. Again, at this
    point, it thinks it’s empty. It hasn’t evaluated anything else. The
    browser figures out the styles and then the div gets painted. The
    browser will determine if it needs to repaint the body—did the element
    get wider or taller? (I suspect there are other considerations but
    width and height changes are the most common effects child elements
    have on their parents.)

    This process continues on until it reaches the end of the document.

    CSS gets evaluated from right to left.

    To determine whether a CSS rule applies to a particular element, it
    starts from the right of the rule and works it’s way left.

    If you have a rule like body div#content p { color: #003366; } then
    for every element—as it gets rendered to the page—it’ll first ask if
    it’s a paragraph element. If it is, it’ll work its way up the DOM and
    ask if it’s a div with an ID of content. If it finds what it’s looking
    for, it’ll continue its way up the DOM until it reaches the body.

    By working right to left, the browser can determine whether a rule
    applies to this particular element that it is trying to paint to the
    viewport much faster. To determine which rule is more or less
    performant, you need to figure out how many nodes need to be evaluated
    to determine whether a style can be applied to an element.


    So why was the stylesheet content not applied progressively (green first, then red)?

    I think the answer is that external stylesheets are parsed as they are downloaded, but not applied until the entire stylesheet has been parsed. Surely, in parsing a stylesheet, the browser optimizes away unnecessary and redundant CSS rules.

    I don’t have any proof to back that up right now, but that explanation sounds reasonable to me and agrees with what you’re seeing, both with external and inline styles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have some text as follows: do this, do that, then this,
Let's say I have table with column 'URL' whrere I store urls like this
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say that I have a set of relations that looks like this: relations
Let's say I have this interface: // .h @interface DataObject : NSObject { NSString*
Let's say that I have classes like this: public class Parent { public int
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say i have this block of code, <div id=id1> This is some text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.