0

How should I go about optimizing CSS code? There's various CSS3 lines, like -moz- and -webkit-, border-top-left-radius, etc.. I believe bigger CSS files increase page load time significantly.

And another question: I've written quite some code, however some of it is left unused. I have over 2000 lines of CSS code, and I bet around 200-300 lines could be removed, perhaps even more. Is it worth revising all the code? It would take quite some time...

6
  • Chrome's dev tools can show you which CSS rules are not used... Commented Jul 21, 2012 at 17:31
  • 1
    Revising your code is not a bad idea. Commented Jul 21, 2012 at 17:32
  • 1
    If you make sure that the CSS files are cached (in the browser), it shouldn't be an issue regardless how big the CSS files are... Commented Jul 21, 2012 at 17:32
  • That is a good point, I do use a caching script. Although, the loading time would be longer for first-time visitors. First impression is the most important one, isn't it? Commented Jul 21, 2012 at 17:40
  • @Jack It's the content that counts. I regularly visit a bunch of sites that are really slow, but I still visit them because I like their content. :) (e.g. Zelda Informer, Replay Hub on Game Informer) Commented Jul 21, 2012 at 17:42

3 Answers 3

4

GZip the files before uploading them on server

It will reduce the files size significantly

Edit: Effect of GZipping -

By gzipping the .css file on Bargaineering, its size dropped from 28.2K to 7.3K, a 74.1% savings.

Sign up to request clarification or add additional context in comments.

Comments

2
  • always remove the last semicolon:

    body { background: black; color: white; }
    to
    body { background: black; color: white }

  • combine multiple properties:

    .class { margin-top: 10px; margin-right : 20px; margin-bottom: 30px; margin-left: 40px; }
    to
    .class { margin: 10px 20px 30px 40px; }

  • use simple colors (instead of `#FFFFFF, #AABBCC, #FF0000 put #FFF, #ABC, #F00)

  • the most important thing: minify your code before uploading on the server. It will remove whitespaces and comments and significantly reduce your code and file size.

3 Comments

margin:10 won't work (it only works with 0). Also, removing the last semi-colon is overkill.
Thanks for the correction. Removed that. I've seen many suggestions about removing the last semi-colon, and I've never seen that someone said it's bad. However, I prefer to put semi-colon always. It's a habit + it's easier to add code later.
Your left and right margin values are backwards, should be 10px 40px 30px 20px in the shortened example.
0

The smaller the file, the quicker it will download and the faster users can render the styles. There are various minifying scripts, I'd check out the YUI Compressor: http://refresh-sf.com/yui/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.