Home / CSSTidy

CSSTidy is a parser and optimizer for Cascading Style Sheets (CSS). Written by Florian Schmitz, CSSTidy is open source and C++ and PHP versions are available. Unlike most other CSS parsers, CSSTidy does not use any regular expressions. As a result of this, CSSTidy has full CSS2 support and is highly reliable.

CSSTidy is available as executable file for Windows, Linux and OSX and can be controlled by command line and as PHP script. The name CSSTidy is an allusion to HTML Tidy to which CSSTidy is considered a counterpart for CSS.

Florian Schmitz is no longer updating CSSTidy (the current version is 1.3) and is looking for a new maintainer for the project.

CSSTidy features

CSSTidy can be used to reformat CSS code, compress CSS code and fix common errors in CSS code such as missing units or semicolons.

(“a” stands for all selectors, “margin” stands for all properties)

  • colours like “black” or rgb(0,0,0) are converted to #000000 or rather #000 if possible. Some hex-codes are replaced by their colour names if they are shorter.
  • a{property:x;property:y;} becomes a{property:y;} (all duplicate properties are merged)
  • margin:1px 1px 1px 1px; becomes margin:1px;
  • margin:0px; becomes margin:0;
  • a{margin-top:10px; margin-bottom:10px; margin-left:10px; margin-right:10px;} becomes a{margin:10px;}
  • margin:010.0px; becomes margin:10px;
  • all unnecessary whitespace is removed, depending on the compression-level
  • all background-properties are merged
  • all comments are removed
  • the last semicolon in every block can be removed
  • missing semicolons are added, incorrect newlines in strings are fixed, missing units are added, bad colors (and color names) are fixed
  • property:value ! important; becomes property:value !important;

Compressing CSS code using CSSTidy

If you use standard whitespace-removal (which removes some white space while still keeping the code readable) you can expect a compression ratio of 30% or more. Whitespace-removal is normally the single most important factor for compression ratio.

Why optimize your CSS code?

Optimized CSS code meas faster loading of your web pages, which is not only beneficial for your visitors but also decreases traffic costs.

CSSTidy can be used to format and fix CSS code to achieve higher browser compatibility. There are four default templates, but you can specify custom templates instead if you want to. Custom templates come in handy when you want to format a lot of CSS code using your own coding style.

What is a parser?

In computer science, parsing is the same thing as syntactic analysis, i.e. the process of analyzing a text to determine its grammatical structure with respect to formal grammar. Interpreters and compilers that checks for correct syntax and builds data structures carry out parsing as a part of their job. Many parses make use of a separate lexical analyzer to create tokens. Some parsers are hand programmed custom made parsers while others are automatically or semi-automatically generated by a tool.