Create Smaller CSS Files
Written by Stefan Vervoort on September 11, 2007This article will tell you how to make your CSS files a bit smaller. You can ask yourself one question; Why shouldn’t you? Read on to find out how to do this.
There are only advantages of making your CSS files some smaller. When your website uses a 5k CSS file, you might not see the difference in loading time, but when using bigger (25k) files, you can notice your website will load a bit faster when using some of the steps below. Another thing is that your file will become more organized and clean. Read on to get tips of improvement.
The main idea of this article is to reduce the number of characters you have used in your CSS. The less characters, the smaller your file.
Comments
You have different ways of using comments. Comments may be something you can not delete in your CSS, because you might forget something (or your fellow-workers). But, there are a couple ways of using them.
/*----------------------*/ /*-----Comment-----*/ /*----------------------*/
These lines of code do the same as:
/*Comment*/
You can still read what it says and what tips it gives. The only thing you did is saving space.
Color prefixes
With some ’standard’ colors it is possible to write their prefix in a shorter way.
color: #ffffff; color: #ff88ff;
This prefix’s will do the same as:
color:#fff; color:#f8f;
Prefixes with all different characters can’t be written shorter. You have to use the same prefix there. For example:
color: #f8f7f2;
Use one line of codes instead of a couple
When you are coding a website by hand, you’ll often first use the padding-left:5px; attribute and some time later a padding-top:10px;. You can write all those in one line of codes.
padding-left:10px; padding-right:20px; padding-top: 0; padding-bottom:30px;
Those codes can be written as simple as this:
padding: 0 20px 30px 10px;
When your padding left and right is the same, and top and bottom too, you can do something else too. Example:
padding-left:10px; padding-right:10px; padding-top: 0; padding-bottom: 0;
This can be written like this:
padding:0 10px;
The same rule applies to margin attributes too. You may also try the same thing with backgrounds, borders and fonts.
You can leave out the ‘PX’ when value is ‘zero’
There’s some logic in this. 0px is the same as 0em or 0pt. We can use just the ‘zero’ here, which will give you another 2 characters of profit.
padding: 10px 0px 0px 0px;
Is the same as:
padding: 10px 0 0 0;
Defining your ID’s and classes
You might have very long ID or Class names. For example, you can name them ‘HeaderMiddleLeft’, but you can also name them something like this, ‘HeaderML’. Sometimes, when using one CSS file with different people, this isn’t very handy, but when your CSS file is for private use, you can shorten those ID’s or classes.
#HeaderMiddleLeft becomes #HeaderML.
Clean up your CSS file
An often overlooked ‘tip’ is to clean your CSS file. Sometimes, I’ve got an idea, but later on, I’ve got a better idea, so I will take the other way. In this coding process, you might create ID’s or classes you don’t use in the final design. You can just delete them!
Create just one element when using the same content in different elements
You might have the a couple elements in your file, with the same content inside it. Those items can be placed ‘inside’ each other. Not clear what I mean? Take a look below to view an example.
h1{ margin:0; padding:0; } h2{ margin:0; padding:0; } h3{ margin:0; padding:0; }
This three elements does the same as simple ‘one’ element;
h1, h2, h3{ margin:0; padding:0; }
Conclusion
On a 25k CSS file, using this small tips could make a difference of more then 5k in size. This means your CSS file, with the same contents, will be loaded 20% faster. This is pure profit. I hope you enjoyed reading this article and watch out for the next one!
Hadjeu!
There are many ways to enhance and learn new ways of doing thing like in old days people only use to trust on HTML coding for making websites. But in today’s world new courses like 220-602, tools and techniques have been introduced like CSS style sheet: which can be reused to add consistency in all the WebPages. For dynamic website people now make and maintain proper database. If you have any experience in the data base or want to enhance Oracle database administrator skill then 1z0-042 certification is perfect choice for you. If you are interested in Cisco networking certification then go for 640-802 courses. After that in order to secure your network 642-523 is best option for you. If you are interested in VOIP services and devices along with networking course then 642-432 will make a good combination. You can also opt for many other courses like 642-176 but select the one according to your interest.









Nice! Useful! Thanks!
Cool summary discussing Create smaller CSS files. Always enjoy this blog.
Thanks for the compliment.
You missed a few if you want to get extreme.
The last statement of css does not require a semi colon (which means if the block only contains 1 rule, no semi-colons are needed:
p{color:#00c;font-weight:bold}
Also carriage returns are also not required, but at least required for readability
This isn’t an extreme guide for cleaning up your CSS file. In my opinion, semi-colons are required to keep the styles readable and easy to edit.
Thanks for sharing your view on this article.
a lot more tips exist but these r good.
“This can be written like this:
Padding:10px 0;”
should be
This can be written like this:
Padding:0 10px;
Nice tips! I will definitely be putting these to use.
Acronyms; Thanks for noticing, changed the article.
Very interesting site. Hope it will always be alive!
Thanx fuer die Infos. werde mal schauen, ob ich noch mehr darueber lesen kann. Falls es noch mehr Infos in deinem Blog gibt, dann waere es nett, wenn du mir die Infos geben koenntest. Oder Quellenangaben.
[...] çœ‹äº†è¿™ç¯‡æ–‡ç« ï¼Œæƒ³æƒ³è‡ªå·±ä¹Ÿç»å¸¸å’Œ CSS 打交é“,对于压缩 CSS [...]
[...] It covers the basic aspect every website needs and absolutely nothing more, which is great for your optimized CSS files as well. * { border: 0; padding: 0; margin: 0; vertical-align: baseline; font-weight: inherit; [...]
You Forgot White space/Carriage returns
Also you can group similar properties of two or more elements that contain the same properties.
White space, yes. I think you should not make your CSS file that hard to scan. You will not be able to find any CSS id’s or classes fast.
The last point, I mentioned that. Look at my last point!
Thanks for your comment.
[...] is a good idea to optimize your CSS files. As I told you before, there are a couple ways to improve your CSS coding. I have found an article studying the benefits and performance of CSS [...]
This is a small part of many CSS tips. But anyway, thanks)
there are tons of advices to compress CSS, but this one is still the base for others
This is a great example to follow!
Very nice indeed.
Thanks
Thanks!! very usefull
Your tip is wonderful! I like it a lot!
Cheers
Excellent, faster page loads means more people that actually bothers to see your page, more people means more income. Good tips, not something i at least go around and think about. I’m glad someone do it for me
Impressive work of guide, and details of step by step, great tips you given and thanks for sharing out your experience on the css.
As I always wanted to try changing my web css file, but have less experience on it. Will try it out again with your guide.
Thanks
I agree it’s good practice to keep your css files as small as possible, though you need to balance this with readability for future editing.
I’m interested to find out whether having multiple stylesheets eg separate stylesheet for menu and content results in greater loading times than a single stylesheet?
[...] View Tutorial [...]