Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External CSS vs inline style performance difference?

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?

like image 731
George Avatar asked Nov 27 '11 08:11

George


People also ask

Does inline CSS affect performance?

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).

Why is inline CSS faster?

Inline CSS means that the CSS is loaded in the <head> tag of the site's HTML. This is faster than the visitor having to download the CSS files directly from the server; however, if all the site's CSS is displayed inline it can actually slow down the load time of the entire site.

Is inline styling slow?

Yes, inline styles are technically faster than an external stylesheet because you are making one less request on top of the page but using an external stylesheet is much preferred for code maintainability.

Do you see any particular advantages to using an external stylesheet vs an inline or internal stylesheet?

Options for styling HTML elements Inline CSS allows you to add styles to specific HTML elements. The internal CSS stylesheet allows you to include CSS code in <head> section of a markup document. External stylesheet works by linking a . css file, containing all the CSS rules, to an HTML document.


2 Answers

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!

like image 55
mauris Avatar answered Sep 19 '22 01:09

mauris


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.

like image 45
sebjwallace Avatar answered Sep 22 '22 01:09

sebjwallace