External CSS vs inline style performance difference?
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.
Related videos on Youtube
George
Updated on October 30, 2021Comments
-
George over 2 years
A friend of mine said that using
<div style=""></div>
instead of compressed css file put aslink href
at the head section gives some performance boost. Is that true?-
Nightfirecat over 12 yearsAs far as I know, they're (roughly) the same, but I can't verify that.
-
Sudhir Bastakoti over 12 yearsThis might shed some light : mathiasbynens.be/notes/inline-vs-separate-file
-
steveax over 12 yearsIt 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 about 10 yearsFor older versions of IE the performance boost is VERY significant. I've seen HTML tables behave like glue with CSS styling.
-
-
Akash Kava over 11 yearsCan 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 over 11 yearsNote that all the disadvantages related to maintenance disappear when there is an engine that inlines the styles before sending to clients
-
stroncium about 11 yearsIt's the essence of caching mechanism. It doesn't need proofs.
-
Ivan Castellanos over 10 yearsIt can be "cashed" doesn't mean is "cashed"; and "essences" are not facts.
-
Sam over 9 yearsPretty 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 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 over 9 yearsIt 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 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 over 9 yearsAh OK, I was referring to non-dynamically added styles
-
Sebas over 9 yearsGuys, it's not duke nukem era anymore, we don't cash people. Cache on the other hand...
-
MindJuice over 9 yearsThe caching argument doesn't hold for single page applications, which generally load everything once up front and generate pages on the fly.
-
Ozymandias about 8 yearsThe disadvantages related to maintenance also disappear when using React + Radium.
-
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 about 8 yearsYes. I was simply adding the comment for those seeing this answer today and beyond.
-
snow over 7 yearsabout 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 over 6 yearsMaybe 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 over 6 yearsI use IE10 and notepad to develop. ?
-
GeekOnCoffee over 5 yearsThis 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 over 4 yearsThat makes maintaining extremely difficult. CSS, JS files should be cached on your user's browser by default.
-
Lee Chee Kiam about 4 yearsMany 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 almost 4 yearsThis 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 over 3 years@s.ermakovich link is now dead
-
Vidar over 3 years@GeekOnCoffee but the HTML page will hopefully be cached (304), so caching isn't really ignored.
-
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 over 3 yearsAlso Note, html pages can be cached as well.
-
Ibrahim Rahhal over 3 yearsless 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 over 2 years'I use IE10 and notepad to develop'... i.e. disregard what i just said.
-
Nick Saunders over 2 yearsCan you explain this recommendation further?
-
Toninho Toddy over 2 yearsyes ... 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></html>