CSSTidy Optimizer is a stylesheet CSS. The latter is used from the command line in Linux and reduces the size of a style sheet CSS according to different criteria, in order to make the display of a more effective website.
1. Optimization
To install CSSTidy, open a terminal as root and run the following command for a rpm-based distribution:
yum install csstidy
Or this one for a Debian-based distribution:
apt-get install csstidy
You can then start csstidy command without any parameters to see options. Some possible examples of optimization:
- compress_colors: using hexadecimal color codes, the property becomes black #000 for example
- discard_invalid_properties: Deletes all invalid lines. This option is
- quite dangerous because one is never safe from a typo (widht instead of width)
- lowercase_s: changes every lowercase selectors
- preserve_css: removes all comments when using the false option
- remove_last_; : Removes the last; each section
For those who do not want to choose specific options, you must know that CSSTidy embarks templates capable of making automatic optimization according to several levels (low, high, highest). An example of use:
csstidy style.css –template = high style.css.out
…
Selectors: 63 | Properties: 198
Input size: 6.24KiB Output size: 3.728KiB Compression ratio: 40.27%
If we compare the number of lines between the two files:
style.css cat | wc -l
368
cat style.css.out | wc -l
51
The difference before and after compression is often very visible. It will recheck all the same stylesheet to make sure that the program has not made any mistakes or deleted important options.
2. Compressing and decompressing a stylesheet
If you wish to then compress a style sheet in a single line, you can use the following command:
style.css cat | tr -d “\ n”> compress.css
And the latter to return to a readable format:
cat compress.css | awk ‘{gsub (/ {|} |; /, “& \ n”); print} ‘> uncompressed.css