Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison of loading CSS inline, embedded and from external files

Tags:

css

We can write CSS as the following types:

  1. Inline CSS
  2. Embedded CSS
  3. External CSS

I would like to know pros and cons of each.

like image 855
RedsDevils Avatar asked Mar 16 '10 15:03

RedsDevils


People also ask

What is the difference between embedded inline and external CSS?

Internal or embedded ⁠— add <style> tag in the <head> section of HTML document. External ⁠— link the HTML sheet to a separate . css file. Inline ⁠— apply CSS rules for specific elements.

What is the difference between external and internal CSS?

Internal CSS are the ones that we can write within the same file i.e the HTML code and CSS code are placed in the same file. External CSS are that we can write in a separate file than the html code i.e the HTML file is separate like(index. html) and CSS file is separate like(style. css).

Why inline CSS has highest priority than external CSS?

Inline style (in HTML style attributes) has the highest specificity and will override any selectors, followed by ID selectors, then class selectors, and eventually element selectors.

What is the advantage of using external CSS as compare to the internal style sheet?

Using external stylesheets, you can apply rules to multiple web pages. If you ever need to make widespread changes to your website design, a single change in the stylesheet can be applied to all linked pages, saving lots of time and effort.


2 Answers

It's all about where in the pipeline you need the CSS as I see it.

1. inline css

Pros: Great for quick fixes/prototyping and simple tests without having to swap back and forth between the .css document and the actual HTML file.

Pros: Many email clients do NOT allow the use of external .css referencing because of possible spam/abuse. Embedding might help.

Cons: Fills up HTML space/takes bandwidth, not resuable accross pages - not even IFRAMES.

2. embedded css

Pros: Same as above regarding prototype, but easier to cut out of the final prototype and put into an external file when templates are done.

Cons: Some email clients do not allow styles in the [head] as the head-tags are removed by most webmail clients.

3. external css

Pros: Easy to maintain and reuse across websites with more than 1 page.

Pros: Cacheable = less bandwidth = faster page rendering after second page load

Pros: External files including .css can be hosted on CDN's and thereby making less requests the the firewall/webserver hosting the HTML pages (if on different hosts).

Pros: Compilable, you could automatically remove all of the unused space from the final build, just as jQuery has a developer version and a compressed version = faster download = faster user experience + less bandwidth use = faster internet! (we like!!!)

Cons: Normally removed from HTML mails = messy HTML layout.

Cons: Makes an extra HTTP request per file = more resources used in the Firewalls/routers.

I hope you could use some of this?

like image 171
BerggreenDK Avatar answered Oct 01 '22 16:10

BerggreenDK


I'm going to submit the opinion that external style sheets are the only way to go.

  • inline CSS mixes content with presentation and leads to an awful mess.

  • embedded CSS gets loaded with every page request, changes cannot be shared across pages, and the content cannot be cached.

I have nothing against either method per se, but if planning a new site or application, you should plan for external style sheets.

like image 30
Pekka Avatar answered Oct 01 '22 17:10

Pekka