Create smaller CSS files

Author: Stefan Vervoort | Published: 11 September 2007 | RSS | LINK Font size:

This 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.

Liked this post? Subscribe, or Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Blogosphere News

18 Comments

  1. PiticStyle
    10:19 am on September 12th, 2007

    Nice! Useful! Thanks!

  2. Cash Advance
    10:20 pm on September 14th, 2007

    Cool summary discussing Create smaller CSS files. Always enjoy this blog.

  3. Stefan Vervoort
    12:27 pm on September 20th, 2007

    Thanks for the compliment.

  4. Wayne Pan
    10:44 pm on September 22nd, 2007

    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 :)

  5. Stefan Vervoort
    8:59 am on September 24th, 2007

    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. :)

  6. Navdeep
    2:24 pm on September 24th, 2007

    a lot more tips exist but these r good.

  7. Acronyms
    1:48 am on October 23rd, 2007

    “This can be written like this:
    Padding:10px 0;”

    should be

    This can be written like this:
    Padding:0 10px;

  8. ZanyPixel
    10:05 am on November 1st, 2007

    Nice tips! I will definitely be putting these to use.

  9. Stefan Vervoort
    3:29 am on November 4th, 2007

    Acronyms; Thanks for noticing, changed the article.

  10. Mateusz
    8:24 pm on November 27th, 2007

    Very interesting site. Hope it will always be alive!

  11. Google Webseiten Optimierung
    3:34 pm on December 21st, 2007

    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.

  12. @ppleçš„éƒ¨è½æ ¼ » Blog Archive » 如何写一个很小的 CSS 文件
    1:47 am on March 3rd, 2008

    [...] çœ‹äº†è¿™ç¯‡æ–‡ç« ï¼Œæƒ³æƒ³è‡ªå·±ä¹Ÿç»å¸¸å’Œ CSS 打交道,对于压缩 CSS [...]

  13. CSS Resets to improve website its browser compatibility » DivitoDesign.com - Webdesign Blog
    11:35 am on March 11th, 2008

    [...] 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; [...]

  14. 3Mushrooms
    6:37 am on May 14th, 2008

    You Forgot White space/Carriage returns :)

    Also you can group similar properties of two or more elements that contain the same properties.

  15. Stefan Vervoort
    1:08 pm on May 14th, 2008

    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. :-)

  16. CSS Compressors, why? » DivitoDesign - Webdesign Blog
    12:31 pm on June 20th, 2008

    [...] 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 [...]

  17. ximx
    9:38 pm on July 15th, 2008

    This is a small part of many CSS tips. But anyway, thanks)

  18. ildvr
    11:25 am on August 3rd, 2008

    there are tons of advices to compress CSS, but this one is still the base for others

Leave a Reply

This blog is a DoFollow blog. This means your URL counts as a backlink. Some basic HTML is allowed. Please keep all comments constructive, polite and on-topic. Any spam or offensive comments will be deleted.