Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal vs External CSS

What's the pros and cons on Internal vs External CSS, thinking of speed, requests, caching etc..? Personally I'm not sure if internal css on dynamic pages will cache..?

like image 215
olemarius Avatar asked Feb 16 '09 11:02

olemarius


People also ask

What is difference between internal and external 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).

Which CSS is better internal or external?

Properties of CSS: Inline CSS has the highest priority, then comes Internal/Embedded followed by External CSS which has the least priority. Multiple style sheets can be defined on one page. If for an HTML tag, styles are defined in multiple style sheets then the below order will be followed.

Is external CSS best practice?

It is considered a best practice to have your CSS stylesheets in an external file. So how can you link that CSS to your HTML file? Linking to an external CSS file is an important part of any HTML page boilerplate.

What is an internal CSS style?

An internal CSS is used to define a style for a single HTML page. An internal CSS is defined in the <head> section of an HTML page, within a <style> element. The following example sets the text color of ALL the <h1> elements (on that page) to blue, and the text color of ALL the <p> elements to red.


2 Answers

Pros for internal CSS: - faster downloads: remember that there will be one additional HTTP request for each external style sheet you have

Pros for external CSS: - it is common for websites to have common 'theme' across all its pages. You can club all such common styles in external file and with one download you get the required style that can be used in multiple pages: saves download time - you can also cache external styles and set an appropriate expiry date.

One thing against for internal CSS is that it can increase the download size of the html.

Best approach: - use mix of internal + external styles depending on which styles are used in diff pages - make sure to set expiry settings on external styles and cache them.

Advantage of combining with Cache expiry settings: "Look and feel" of web apps is governed by the following:

  • you typically want to maintain same 'feel' across all pages
  • the content is more likely to change frequently than the styling

If you put styles in external CSS file and set a cache expiry of say 1 month, then during this time all users will have very low 'start' delays because only the content that has changed will be downloaded: the styles will be reused from your browser cache. The browser will request a refresh automatically the first time someone tries to access your page after the expiry date.

like image 66
Sesh Avatar answered Oct 26 '22 11:10

Sesh


If the page is cachable, the internal CSS for this page is also cachable (as it is part of the page). But external stylesheets have the advantage that they can be used for many pages and are only requested once when cachable.

Therefor you first have an additional request (the external stylesheet) but then less data to transfer on further requests.

like image 28
Gumbo Avatar answered Oct 26 '22 12:10

Gumbo