Improving your website includes a few steps. In this article I will be discussing how to implent a php snippet that will gzip your css file to the browser. This little piece of code will be attached to your css files with the auto_prepend_file module in a little .htaccess file.
Note that if you have access to your apache server, there is an easy Gzip module that will do this trick for you. If you do wish to gzip through PHP there are several ways, this being one of them. In other articles I discuss different methods.
The first step is to copy the following code into a php file called cssgzip.php and copy it in the same directory as your stylesheets are in. If you have multiple folders with stylesheets you can copy the phpfile to each folder.
<?php ob_start ("ob_gzhandler"); header("Content-type: text/css; charset: UTF-8"); ?>
The ob_gzhandler tells php to compress the stylesheet in gzip format. The header then tells how to interpret the compressed file, in this particular case it is CSS.
Now it is time to tell your server to apply this snippet to your css files. As mentioned before, the php file has to be in the same directory as your CSS files. Now make a new empty .htaccess file for the same directory and copy the following code to it.
AddHandler application/x-httpd-php .css php_value auto_prepend_file cssgzip.php php_flag zlib.output_compression On
This code tells your server to apply the php file to each css file in the folder. Remember to put both the .htaccess file and the php file in the same folder as your css files.
This will do the trick, you can check the result on pages like GTmetrix in the gzip compression section.