DivitoDesign

Everything for the Web Professional

9 Tips to Smaller & Optimized CSS Files

Information about the Author:

Name:

URL: http://twitter.com/divitodesign

Description enjoy design, write articles, like typography, love football, keep chilling.

More on: CSS

34 Comments

  1. I agree with the principle of #6, but saving a few characters, IMO, is not worth less-than-clear class names (e.g., “h-cats”). For the sake of future enhancements to the code (not to mention the ability of another coder picking up the code later), I think more descriptive class names (like “header-categories”) is worth a couple extra characters in the code, especially if coders follow the other recommendations which you have helpfully provided :)

    Thanks for the good article.

  2. A few more suggestions:

    Trim leading and trailing zeros:
    - 0.90em can be shortened in two places and just be written .9em

    Background positions:
    - ‘top left’ is the default position, so that combination can be stripped completely. In the same way, background-repeat is by default ‘repeat’.
    - Replace top left center right with 0% 0% 50% 100% respectively.

    I always remove unneccessary spaces, tabs and lineshifts. It’s easy if you just keep one uncompressed version of your CSS file and one compressed file that will cache and regenerate if changes happen in the original file.

    Some colors are shorter if you write their name rather than their hex value, for instance:
    - red, coral, gold, green, ivory, linen etc.

    A good compressor should also be able to combine identical CSS rules or selectors:
    - #main{color:red} and #sub{color:red} becomes #main,#sub{color:red}
    - #main{color:red} and #main{weight:bold} becomes #main{color:red;weight:bold}

  3. @existdissolve – I agree with what you say. You could, for example, document the choice of class/id’s, but that give you extra chars too. You should make things clear yet small! Thanks for your comment.

    @Torkil Johnsen – I agree with you on the Trim leading/trailing zero’s. It’s a valid point, that I think isn’t to farfetched.

    I think the other point you made is valid too, although that’s when you are getting very extreme with CSS optimizing.

    I have to point you to an accessibility issue that normal “color names” have. They don’t display correctly on several browsers.

    Thanks for your thoughts!

  4. Kris Brixon says:

    I agree with #3 and #5, but in general you want to write things for maintainability. Use a compressor, zip and cache for performance. One way to reduce the amount of code is to have the CSS actually Cascade which you are eluding to in #3.

  5. Thilo says:

    Does CSS compression still make sense when you have apache server compression (gzip)?

    I personally don’t like CSS compressors because they prevent me from doing quick changes on a live server when needed and oftentimes they also cause errors.

    For example:

    h2.myclass {
    margin-top: 1em;
    margin-right: 1em;
    }

    does not necessarily have to be margin: 1em 1em 0 0;

    it can be! but unless other values have been inherited from h2.

    But css compression is a nice protection against ripping from rookies. Some just see the source and say “uhm ahhh – no thanks!”

  6. Kris Brixon says:

    @Thilo – gzip will not compress as much if the file has already been through a CSS cruncher/compressor. If I had to pick only one of the two, then I would use gzip. However, doing both is fine too.

    Make sure Apache has the headers set right so the file will be cached on the client’s machine.
    http://developer.yahoo.com/performance/rules.html

  7. Alex says:

    I honestly think these most of these are completely pointless… Inheritance being the only good tip. Especially with default gzipping. As for CSS compressors stopping rookies stealing your CSS, don’t be so sure… thanks to Firebug.

  8. [...] Posted an item on Worth Reading. 9 Tips to Smaller & Optimized CSS Files » DivitoDesign [...]

  9. Cleo says:

    Writing the code in one line when this have just one propriety:
    html {overflow:scroll}

  10. yedpodtrzitko says:

    I do not agree with #8. Last rule is not permanently last. When you add new rule, you have to think about missing semicolon before. I consider it annoying and it’s bad habit, imho.

    If you use some revision controlller system, it unnecessarily marks added semicolon as changed line too.

    So if you want to compress CSS/JS, why to try hard if compressor can do it better?

    Personally, I use two versions of CSS/JS files – one is compressed for deploy on production server, one is bloated for developement and debugging on localhost

    (sorry my english)

  11. [...] 9 Tips to Smaller & Optimized CSS Files » DivitoDesign. Tags: css, tips [...]

  12. [...]   9 Tips to Smaller & Optimized CSS Files ??|Author?Stefan Vervoort ??|Trans? [...]

  13. Your page file size is smaller since it doesn’t have style rules associated with CSS.

  14. I agree with @yedpodtrzitko, and strongly disagree with #8. I agree that it is good to develop with optimization in mind. I think all of your other points are valid, but I think you are encouraging bad habits.

    I have helped other developers/designers with CSS problems before and have taken over CSS code in which the last semicolon was omitted. In several of these cases, I’ve probably spent hours trying to find the source of an issue, only to find that a property had been added to a selector after the previous “last” property that didn’t have a semicolon.

    If you had 50 selectors, you would be saving 0.0488 KB. I don’t think its worth the trouble for other developers to worry whether or not a semicolon is there.

    As @yedpodtrzitko said, if a CSS compressor strips this off for you amongst other optimizations why not just let the compressor handle this for you?

    My 2cents.

    • @yedpodtrzitko& @Lloyd – True. Absolutely. I should have said you could delete the semi-coloms after you are done coding, which was the original meaning of #8.

      Thank you for your comments people, enjoyed reading them!

  15. Jabz says:

    Nice list, although I could not find anything new for myself. A friend of mine started a css-size-optimization engine. You can find it a csscruncher.com.

  16. Fabien says:

    Hey Stefan, the biggest issue with #3 is the fact that combining elements means that it makes it difficult if you want to make changes to individual elements such as h1, h2 and h3 when you do not want the changes to apply to all of the headings. Great post nonetheless, I pointed out the issue because I am editing a WordPress theme at the moment and I am having to make more changes as a result of the combination of styles of the elements. Speak to you on GoogleTalk soon! Thanks for the article.

  17. Gochi says:

    This is GOOD!
    I’m just starting my HTML self-phase training.

    Isn’t handled by HTML codes? or is HTML has this kind of functionality? Another question is what if I’ll be using Javascript? will there be difference in my coding? Now I am confused!
    its really a newbie ideas. Thanks

  18. Not bad to shorten all the css stuff but I shall not go with it. Perhaps most of the time we code for others and our code should be readable for rest of the world. It means, we are not going to code or to handle the code forever so we should follow some standard(a standard which should be feasible for all).

  19. Hi Stefan,

    Your post is great, great and great.
    All notes you made is very common and every designer knows already but still no one follow it.

    But it really helpful in loading time.
    I just cleaned my osCommerce website css and it seems to become more faster in loading css file.

    Once again many many thanks

  20. Gopal Raju says:

    Nice Article. I am not sure whether it’s a good practice to remove ; from the last line.

    Gopal,
    http://www.productivedreams.com
    http://www.twitter.com/gopalraju

  21. Sid says:

    Good tips! Useful for newbie web design people!

  22. I am wondering what would be the effect of it on performance? I think it is quite easy to manage all that in grouped position so it definitely has benefits. I always like clean code and thanks to explain how can we do it.

  23. Chad says:

    I have a few sites but have always been afraid of editing a css file, so I would always buy a template with the css the way I like it, but now I know I can edit the colors and background colors of my css file. I did not know it was that easy. This will help to me to easily change the color to give my site a fresh look without having to change the whole layout.

    thanks,
    Chad

  24. Web Design says:

    All good tips – I guess everyone has their own style when writing css. I have found numbering each section and including a commented out table of contents at the top of the document quite useful when coming back to edit an old file. It also helps me find my way around those larger css documents whilst working on them.

  25. Don says:

    Thanks for the tips. I’m a beginner in web design and honestly I didn’t know that I need to optimize the CSS files. This post is enlightening for me.

  26. I have been learning wordpress design structure since 3 months and have designed many of my blogs myself. We should have very well managed and sophisticated code and that is basic of coding. I love these tips as they are pretty good to keep css files clean.

  27. CSS Styles says:

    Nice Post! Optimizing CSS file is really a tough but interesting thing to do. Thanks for sharing such info, which will help me in doing this easily.

  28. Informative sharing stefan i coll list of css tips i appreciate your effort.

Please Respond and Give your Opinion

We use Gravatar to handle our avatars. Get one there!