Does the order of rules in a CSS stylesheet affect rendering speed?

后端 未结 6 1514
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-13 17:45

While this could possibly result in a simple yes or no answer I\'ll go for it anyway


Consider the following example:

HTML

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 18:36

    After some more testing and reading I came to the following conclusion, no, it does not matter. Even after some ‘extreme’ testing, I could not find anything that supports the idea that the order matters.

    There were no 'flashed of unstyled content' or the likes, it just took way longer to load the page ( way way longer :D )

    Tests I ran I created a test page with 60.000 div elements, each having a unique ID attribute. Each of these ID’s had their own css rule applied to it. Below that I had a single span element with a CLASS attribute, which was also had a css rule linked to it.

    These tests created a html file of 2MB with a corresponding css file of 6MB.

    At first I attempted these tests with 1.000.000 divs and css rules, but Firefox did not approve and started crying, begging me to stop.

    I generated these elements and their css with the following simple php snippets.

    
    ";
        }
    
    ?>
    

    And

    "; } ?>

    The result was put in a html and css file afterwards to check the results.

    Mind you, my browser ( Firefox 5 ) really did not appreciate me playing around with this, it really had some issues generating the output, the occasional this program is not responding message was not afraid to show it's face.

    These tests were ran on a localhost, ran by a simple XAMPP installation, it might be possible that external servers result in a different resultset, but I am currently unable to test that.

    I tested a few variations on the above:

    • Placing the element before all the generated divs, in the middle and at the end
    • Placing the span’s css definition before, in the middle or at the end of the css file.

    Oh and may I suggest: http://www.youtube.com/watch?v=a2_6bGNZ7bA while it doesn't exactly cover this question, it does provide some interesting details about how Firefox ( and possibly other browsers )work with the stuff we throw at it.

提交回复
热议问题