CSSTidy - for users
If you simply want to optimise your CSS code, go to the online version of CSSTidy. This is the fastest and easiest way to optimise your code.
CSSTidy (command line)
This is the syntax for CSSTidy:
csstidy input_filename [ --preserve_css=[false|true] | --remove_bslash=[true|false] | --compress_colors=[true|false] --lowercase_s=[false|true] | --timestamp=[false|true] | --optimise_shorthands=[1|2|0] | --remove_last_;=[false|true] | --sort_properties=[false|true] | --sort_selectors=[false|true] | --merge_selectors=[2|1|0] | --compress_font-weight=[true|false] | --allow_html_in_templates=[true|false] | --silent==[false|true] | --case_properties=[0|1|2] | --template=[default | filename | low | high | highest] | output_filename ]*
So to optimise a CSS file and send the output to screen use
csstidy mycssfile.css
If you want to save the output to a file, use csstidy mycssfile.css myoutputfile.css
If you use the filename "-", CSSTidy reads from stdin. To change settings, you have to add "--thesettingyouwanttochange=true or false" after the input file. If "true" is first in the syntax description, this indicates that "true" is the default value (the same applies to false). Example:
csstidy mycssfile.css --remove_last_;=true myoutputfile.css
CSSTidy - for developers (PHP)
The following is a basic example how to use CSSTidy in PHP:
<?php include('class.csstidy.php'); $css_code = ' a { color:black; background-color:blue; }'; $css = new csstidy(); $css->set_cfg('remove_last_;',TRUE); $css->parse($css_code); echo $css->print->formatted(); ?>
Another important method is load_template($string)
/ load_template($filename,TRUE)
. This you can use to load a custom template from string or from file. More information on templates are available. Note that you should not use CSSTidy to do a "live" optimisation of all your CSS code since the optimisation is quite slow if the stylesheet has a certain size. There also is a full documentation.
Important Usage Notes
All comments and most browser specific hacks will be removed if "preserve CSS" is not enabled (also see a list of hacks that work). Although you should only enter valid (or rather well-formed) code, CSSTidy can also parse and fix incorrect code.
If you consider changing the default settings, keep in mind that usually the best options are chosen by default so that the compression is good and the chance that the stylesheet gets another effect low (which may happen for example if you sort the selectors).