Reputation: 33
That's it. I've been hearing about CSS files a lot.
What are the main advantages of having a CSS file instead of writing the styles in the HTML code directly?
Upvotes: 1
Views: 921
Reputation: 49
Versioning becomes far easier as you have a central point to apply changes. The loading time of your site advances because you only deliver the stylecode ONCE and not with every html page you deliver. Furthermore you save up loading time as the css can be cached locally and so the site loads faster after first load, if there was no changes. This can also cause problems, see solution for those problems in point 2. you can also use different styles for different platforms or different tasks (such as braille or print) see available types here: Media types
There are severe cache problems regarding Internet Explorer, you can give version numbers to keep the cached css out of order, if there were changes applied, so there is NO disadvantage of using css files but a HUGE advantage in administering the site. Example of versioning:
<link rel="stylesheet" href="[path_to_css]/style.css?v=[date]" type="text/css">
So there are only huge advantages and no disadvantages of using css, so it is best practice.
Upvotes: 1
Reputation: 1408
I'd add to JohnP's excellent answer by saying:
Upvotes: 4
Reputation: 1217
Some good answers by JohnP. However, the most important reason for me would be the separation of presentation and content.
Upvotes: 3
Reputation: 1935
There are a few advantages;
1) You can re-use the CSS in different pages across your site.
2) The download is separate for CSS when it is in a separate file, this is quicker.
3) The separate CSS file will be treated as static content and likely cached locally. Again quicker.
I personally find CSS easier to read and edit when it is in its own file.
Upvotes: 3
Reputation: 50029
Assuming your site has 10 pages
You don't have to repeat yourself 10 times.
If your style changes, you don't have to do the change in 10 files
Your HTML files are smaller
Your CSS files can be cached
You can reuse the style sheet on other sites you make
Upvotes: 15