Rendering styles
The question is - what will a browser do, given a page with several stylesheets, each of them probably overwriting definitions from the previous ones? Will the browser render the page using the first received css file, while downloading the other ones and after that partially re-rendering where required? The answer is: no, the browser will wait until all CSS files are downloaded, (then probably merge all definitions, just a wild guess) and will render once.
Test
I did this test - one page with two stylesheets that contain pretty much the same selectors for different table styles (thanks to this gallery). Each of the CSS files is actually a PHP script and has a call to sleep(), one sleeps for 5 seconds, the other one for 10.
Result
The browser sits there and waits for the both styles, rendering nothing (except for the page title, but that's not really rendering, is it?). So nothing happens for 10 seconds, then the second style is used for the final rendering. This happens in both FF and IE.
Misc
I also tried sleeping in the actual page, and flushing the output after each row. In my home environment FF renders each row as it's received, but in my hosted environment, it waits for the whole table. IE alsways waits for the complete table.
If I put the page to too much sleep so that the php script dies before the second stylesheet is dowloaded, the browser uses whatever is at hand (css1) to render the page.
Demo/download
- the actual test - two CSS files, first one sleeping 5 seconds, the other one 10
- table with no styles, sleeping 1 second after each row
- table with css1 only, css sleeps 5 seconds
- zipped test for your testing pleasure
Post this entry to: » del.icio.us » Digg » Furl » Newsvine » reddit » Y!
June 17th, 2007 at 10:42 pm
[…] The thing is, when it comes to performance, the browser won't render any part of the page, until all stylesheets are downloaded (fiddled with here). That includes, unfortunately, stylesheets not designed for the currently rendered media. In other words, the browser won't display your page, until the print stylesheet is also downloaded, although it's not used at all for displaying the page. That sucks and should really be addressed in future browser versions. […]
June 21st, 2007 at 6:23 pm
This article always speaks about "the browser" but in the last paragraphs we learn that it's just FF and IE (which versions?). As far as I know, there are no recommandation of W3C on the subject, so each browser is free to act as it wants to. For instance, have a look at Opera8/9, it behaves differently. And Safari or konqueror or new versions of FF might be smart, too.
June 21st, 2007 at 6:32 pm
Thanks Mytskine for the comment. You're right - Opera does render the page, then re-renders when the first css arrives, the does it again when the second css arrives.
I tested again with the latest IE, FF and Safari (on Windows) - they all wait for all css files, before rendering. It makes sense in a way, because re-rendering the page can be expensive and doing it several times in one request is probably something browsers want to avoid.
June 23rd, 2007 at 9:02 pm
[…] Another idea - as shown in a previous post, the browsers (except Opera) won't show anything until the last bit of CSS is downloaded. So if you put you big-ass CSS file at the bottom of the page (which is exactly the opposite of what you should be doing when aiming at performance) you might also achieve the desired flicker-free rendering. Post this entry to: » del.icio.us » Digg » Furl » Newsvine » reddit » Y! […]