开发者

Comparison of loading CSS inline, embedded and from external files

开发者 https://www.devze.com 2022-12-23 06:08 出处:网络
We can write CSS as the f开发者_开发百科ollowing types: Inline CSS Embedded CSS External CSS I would like to know pros and cons of each.It\'s all about where in the pipeline you need the CSS as I

We can write CSS as the f开发者_开发百科ollowing types:

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

I would like to know pros and cons of each.


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?


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.


Inline

Quick, but very dirty

This is (sadly) also the only really sane option for HTML formatted email as other forms often get discarded by various email clients.

Embedded

Doesn't require an extra HTTP request, but doesn't have the benefits of:

Linked

Can be cached, reused between pages, more easily tested with validators.


You want external css. It's the easiest to maintain, external css also simplifies caching. Embedded means that each separate html file will need to have it's own css, meaning bigger file size and lots of headaches when changing the css. Inline css is even harder to maintain.

External css is the way to go.


Where to start!!??

Say you had a site with 3 pages... You could get away with Inline CSS (i.e. CSS on the page itself, within tags).

If you had a 100 page website... You wouldn't want to change the CSS on each page individually (or would you?!)... So including an external CSS sheet would be the nicer way to go.


Inline CSS is generally bad. It's much easier to modify the style of a page when all the styles are located in one central location, which inline CSS doesn't offer. It's easy for quickly prototyping styles, but shouldn't be used in production, especailly since it often leads to duplicating styles.

Embedded CSS centralizes the styles for the page, but it doesn't allow you to share styles across pages without copying the text of the embedded style and pasting it in each unique page on your site.

External CSS is the way to go, it has all of the advantages of embedded CSS but it allows you to share styles accross multiple pages.


Use external CSS when:

  • you have a lot of css code that will make your file messy if you put it all inline
  • you want to maintain a standard look-and-feel across multiple pages

External CSS makes it a lot easier to manage your CSS and is the accepted way of implementing styles.

If the styles are only needed for one file, and you don't foresee that ever changing to apply to other pages, you can put your css at the top of the file (embedded?).

You should generally only use inline CSS if:

  • It's a one-time formatting for a specific tag
  • You want to override the default css (set externally or at the top of the file) for a specific tag


To everyone in the here and now, reading after 2015, with projects like Polymer, Browserify, Webpack, Babel, and many other important participants that I'm probably missing, we have been ushered into a new era of writing HTML applications, with regards to how we modularize large applications, distribute changes and compose related presentation, markup and behavior into self-contained units. Let's not even get started with service workers.

So before anyone forms an opinion on what is and isn't feasible, I would recommend that they investigate the current web ecosystem in their time to see if some issues related to maintainability have been gracefully solved.


Pros:

Allows you to control the layout of the entire site with one file. Changes affect all documents at the same time. Can eliminate redundant in-line styling (Font, Bold, Color, Images) Provide multiple views of the same content for different types of users.

Cons:

Older browsers may not be able to understand CSS. CSS is not supported by every browser equally. In this case, the pros far outweigh the cons. In fact, if the site is designed in a specific way, older browsers will display the content much better (on average) than if the site were managed with tables.


If you are using a server side language, why not embed CSS and use conditional programming to display it as necessary? For example, say you're using PHP w/ Wordpress and you want some homepage specific CSS; you could use the is_home() function to show your CSS in the head of the document for that page only. Personally, I have my own template system that works like so:

inc.header.php = all the header stuff, and where page specific CSS would go I put:

if(isset($CSS)) echo $CSS;

Then, in a specific page template (say about.php), I would use a heredoc variable for the page specific CSS, above the line which includes the header:

Contents of about.php:

<?php

$CSS = <<< CSS

.about-us-photo-box{
/* CSS code */
}

.about-us-something-else{
/* more CSS code */
}
CSS;

include "inc.header.php"; // this file includes if(isset($CSS)) echo $CSS; where page-specific CSS would go ...

<body>

<!-- about us html -->

include "inc.footer.php";

?>

Is there something I'm missing that makes this approach inferior?

Pros:

1) I can still cache the page using standard caching techniques (is there a caching method that takes advantage of a CSS only external file??).

2) I can use php for special case conditional formatting in specific class declarations if absolutely necessary (PHP doesn't work in a CSS file, unless I'm missing some server directive I could set).

3) All my page specific CSS is extremely well organized in the actual PHP file in which it's being used.

4) It cuts down on HTTP requests to external files.

5) It cuts down on server requests to external files.

Cons:

1) My IDE program (Komodo IDE) can't follow the Heredoc formatting to properly highlight the CSS, which makes it slightly harder to debug syntax errors in the CSS.

2) I really can't see any other con, please enlighten me!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号