Larger js and css files waste not only your bandwidth, but also your visitors’ time, so it’s win-win to minify them.
There’re 2 ways to minify a file, the first is to reduce the content length, like removing useless content, and the other is to deliver a compressed version, which is very effective for text content. We can use them both on js and css files.
1. Reduce the content length
The simplest way is using some online tools,
- For css, you can use http://www.cleancss.com/.
- For js, you can use http://dean.edwards.name/packer/.
If you are familiar with css and js, you could optimize the code at first:
- Remove the css rules and js functions that are not actually used anywhere.
- Simplify code:
- For css, you might see such rules: .class #id { // some rules } , does #id{// some rules} works the same way?
- For js, you might see: var a=1; var b=2; , this can be simplified as var a=1,b=2.