External CSS vs inline style performance difference?

77,554

Solution 1

The performance boost that your friend mentioned is probably too trivial compared to the amount of performance boost (through other factors) using a CSS file.

Using the style attribute, the browser only paints the rule for that particular element, which in this case is the <div> element. This reduces the amount of look up time for the CSS engine to find which elements match the CSS selector (e.g. a.hover or #someContainer li).

However, putting styling at element level would mean that you cannot cache the CSS style rules separately. Usually putting styles in CSS files would allow the caching to be done, thus reducing the amount of load from the server each time you load a page.

Putting style rules at the element level will also make you lose track of what elements are styled what way. It might also backfire the performance boost of painting a particular element where you can repaint multiple elements together. Using CSS files separates the CSS from HTML, and thus allows you to make sure that your styles are correct and it's easier to modify later on.

Therefore if you look at the comparison, you would see that using a CSS file has much more benefit than styling at element level.

Not to forget when you have an external CSS stylesheet file, your browser can cache the file which increases your application efficiency!

Solution 2

The page will load faster if you use inline styles vs style sheets. In some cases must faster.

When you use a style sheet using href it requires another request to the server, then the parsing of the file after response. With inline styles there is none of that, just direct parsing.

If a client has slow internet then that single request could be very slow leaving the page style-less until the style sheet get delivered. Again, if it were inline there would be no delay at all.

The only reason we use style sheets is to be organised. There are times when they are not needed, so inline styles or in-document style sheets suffice.

Solution 3

It's not an easy question to answer, because the perfomance in this case depends on many factors (complexity of CSS selectors, document size, etc.). However, if we take an isolated case, then we can see that CSS class is in general faster than inline style:
Inline style vs CSS class

Solution 4

Well it can but the reason for the linked or external style sheet is so it can be cached in the browser especially when your using the same div in multiple pages for the site. This means the browser only has to load the style sheet once instead of having to reload the code every time the browser reloads page. It also makes for cleaner code which makes any changes or debugging easier.

Solution 5

There is no fixed answer in my opinion.

An inline CSS will load faster if the CSS content size downloads faster than your server would respond to an external CSS file request (considering DNS time, server latency, etc).

For normal size CSS I would inline them in the page, for something over 15-20KB I would probably put it in an external file and make sure it can be cached.

I am sure there are many other considerations I am missing now, but there is no fixed answer for inline vs external.

Share:
77,554

Related videos on Youtube

George
Author by

George

Updated on October 30, 2021

Comments

  • George
    George over 2 years

    A friend of mine said that using <div style=""></div> instead of compressed css file put as link href at the head section gives some performance boost. Is that true?

    • Nightfirecat
      Nightfirecat over 12 years
      As far as I know, they're (roughly) the same, but I can't verify that.
    • Sudhir Bastakoti
      Sudhir Bastakoti over 12 years
      This might shed some light : mathiasbynens.be/notes/inline-vs-separate-file
    • steveax
      steveax over 12 years
      It would have to be a significant performance boost to deal with the maintenance nightmares that are inline styles. So far I've seen no eviidence of thatt.
    • Jonathan
      Jonathan about 10 years
      For older versions of IE the performance boost is VERY significant. I've seen HTML tables behave like glue with CSS styling.
  • Akash Kava
    Akash Kava over 11 years
    Can you provide any statistics with chrome profiler? CPU & GPU are costly on mobile and tablet, where battery consumption is huge cost. I think there is a trade off. And what is the benefit of Cache? Browser will have to compute style for current element and resolving styles will be more costly if there are many rules.
  • Moshe Shaham
    Moshe Shaham over 11 years
    Note that all the disadvantages related to maintenance disappear when there is an engine that inlines the styles before sending to clients
  • stroncium
    stroncium about 11 years
    It's the essence of caching mechanism. It doesn't need proofs.
  • Ivan Castellanos
    Ivan Castellanos over 10 years
    It can be "cashed" doesn't mean is "cashed"; and "essences" are not facts.
  • Sam
    Sam over 9 years
    Pretty sure this is testing the speed at which the node class or style attribute is changed rather than the speed at which the style is applied which is what the question is asking
  • s.ermakovich
    s.ermakovich over 9 years
    @Sam What do you mean by the "speed at which the style is applied"? Can you provide a test which measures this speed? If you take a look at the test code, you will see that page reflow occurs on each iteration, which means that test covers not only class/attribute modification, but also actual impact on the page.
  • Sam
    Sam over 9 years
    It maybe includes the time to add the style but it also adds time. What if the document already had the class test or an inline style without JS needing to add it. I'm getting at the speed for the CSS to be resolved in either case and (I might be wrong) but I don't think this test just does that, I think it does extra work.
  • s.ermakovich
    s.ermakovich over 9 years
    @Sam This test ignores possible footprint of loading CSS by browser and processing it. I mentioned in my answer that this is an isolated case. It just measures performance of applying styles using different ways (inline vs external). This is what typical web applications do today - change HTML document from JavaScript without page reload.
  • Sam
    Sam over 9 years
    Ah OK, I was referring to non-dynamically added styles
  • Sebas
    Sebas over 9 years
    Guys, it's not duke nukem era anymore, we don't cash people. Cache on the other hand...
  • MindJuice
    MindJuice over 9 years
    The caching argument doesn't hold for single page applications, which generally load everything once up front and generate pages on the fly.
  • Ozymandias
    Ozymandias about 8 years
    The disadvantages related to maintenance also disappear when using React + Radium.
  • mauris
    mauris about 8 years
    @AjaxLeung no doubt what you and Shaham had said is is true. This answer was written in 2011, when most modern tools were not available yet.
  • Ozymandias
    Ozymandias about 8 years
    Yes. I was simply adding the comment for those seeing this answer today and beyond.
  • snow
    snow over 7 years
    about the caching issue. Don't html files get cached? And if they do, wouldn't the inline CSS inside them get cached with them too?
  • Felipe Alameda A
    Felipe Alameda A over 6 years
    Maybe at that moment this response was accurate. At this time, try Google SpeedPage to confirm otherwise. For PHP scripts, for example, stylesheets are selected, minified and previously included (include()) in the page. So the page scripts, those that the server compiles and delivers to the browser, are never modified directly. You only work with independent stylesheets. In my experience, to achieve a 100/100 rating in PageSpeed you usually have to use inline styles.
  • AlexGrafe
    AlexGrafe over 6 years
    I use IE10 and notepad to develop. ?
  • GeekOnCoffee
    GeekOnCoffee over 5 years
    This answer completely ignores browser caching. Stylesheets allow for only requesting the styles once and caching them, decreasing the amount of content sent over the wire.
  • dave2118
    dave2118 over 4 years
    That makes maintaining extremely difficult. CSS, JS files should be cached on your user's browser by default.
  • Lee Chee Kiam
    Lee Chee Kiam about 4 years
    Many web applications use webpack style-loader (which produce inline style). Should we stop to use style-loader based on this answer?
  • Jan Van der Haegen
    Jan Van der Haegen almost 4 years
    This answer also ignores the fact that inline styles make the HTML file larger. Potentially, especially when using repeated inline styling, a number of times larger than CSS file. By the way, browsers can load multiple files in parallel (and cache them like mentioned above).
  • cobrexus
    cobrexus over 3 years
    @s.ermakovich link is now dead
  • Vidar
    Vidar over 3 years
    @GeekOnCoffee but the HTML page will hopefully be cached (304), so caching isn't really ignored.
  • Vidar
    Vidar over 3 years
    @JanVanderHaegen The initial load time might be faster if you don't have to include CSS that isn't used on the particular page you are visiting. If you put all of your CSS in external files then the HTML file is smaller, true, but the external CSS file might be larger. Pros and cons.
  • Muhammad Umer
    Muhammad Umer over 3 years
    Also Note, html pages can be cached as well.
  • Ibrahim Rahhal
    Ibrahim Rahhal over 3 years
    less html doesn't mean less load, It's differently a factor but the first contentful paint needs your CSS to be loaded as well.
  • run_the_race
    run_the_race over 2 years
    'I use IE10 and notepad to develop'... i.e. disregard what i just said.
  • Nick Saunders
    Nick Saunders over 2 years
    Can you explain this recommendation further?
  • Toninho Toddy
    Toninho Toddy over 2 years
    yes ... inside head use <style> tag to avoid http request and write your core css such as preloading overlay, skeleton screens, etc ... this way as soon as server responds will paint something on screen instead of blank page. EX: <html><head><style>/* core css */</style></head><body><!-- content --><link href="external-stylesheet.css"/><script></script></body></ht‌​ml>