- Web Design Tutorials, Tips, Top Resources

CSS Optimization, Compression: Crunch Your CSS with PHP

by Shirley Kaiser, SKDesigns

Published January 26, 2008. Copyright © 2008 Shirley E. Kaiser, M.A., SKDesigns and All rights reserved.

The PHP code below is an example of how to dynamically optimize your CSS server-side using PHP. This example uses GZIP compression to compress the CSS file and the 'compress' function to remove extraneous whitespace (carriage returns, extra spaces).

This approach allows you to work with your CSS formatted as you wish locally but automatically (dynamically) serves an optimized, compressed version to your site visitors - an ideal approach for many who prefer to work with multi-line CSS, those who don't want to manually add and remove extraneous whitespace whenever the CSS needs tweaking, or both.

This is just one of many possible approaches to compressing and crunching your CSS. For example, you can also configure server-side compression for Apache servers via your server's .htaccess file or .httpd.conf file, provided you have access (many of us using shared servers from our ISPs have access to the .htaccess file only).

You are welcome to use the example PHP code shown below if desired. Note that the example below is fairly generic and you may need to add to or modify the rules for your particular site's CSS. This PHP code example is provided with the assumption that whoever uses it is already familiar with PHP.

Of course, in order for this to work, your server must be configured to use PHP, and you must be able to use PHP within your CSS.

/* ---------------------------------
26 January, 2008 - 2:55pm:
<!-- -->
Adapted for by Shirley Kaiser, SKDesigns

1. The cache-control and gzip compression is adapted from
The Definitive Post on Gzipping your CSS
by Mike Papageorge,
2. Function compress is adapted from
<!-- -->
which removes extraneous whitespace: line breaks, carriage returns,
plus CSS comments.

This PHP code goes at the very TOP of the PHP-enabled style sheet
above EVERYTHING else.
--------------------------------- */
/* initialize ob_gzhandler to send and compress data */
ob_start ("ob_gzhandler");
/* initialize compress function for whitespace removal */
/* required header info and character set */
header("Content-type: text/css;charset: UTF-8");
/* cache control to process */
header("Cache-Control: must-revalidate");
/* duration of cached content (1 hour) */
$offset = 60 * 60 ;
/* expiration header format */
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s",time() + $offset) . " GMT";
/* send cache expiration header to broswer */
/* Begin function compress */
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, new lines, etc. */        
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
/* remove unnecessary spaces */        
$buffer = str_replace('{ ', '{', $buffer);
$buffer = str_replace(' }', '}', $buffer);
$buffer = str_replace('; ', ';', $buffer);
$buffer = str_replace(', ', ',', $buffer);
$buffer = str_replace(' {', '{', $buffer);
$buffer = str_replace('} ', '}', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(' ,', ',', $buffer);
$buffer = str_replace(' ;', ';', $buffer);

About the Author

 Deliver First Class Web Sites: 101 Essential Checklists, by Shirley Kaiser. SitePoint (07-2006).  Shirley Kaiser owns SKDesigns, a Web site design and development business she started in 1996. She specializes in accessibility-friendly, user-centered Web site design and graphics, Web standards, information architecture, Web site speed optimization, and collaborative team projects. Shirley writes weekly columns, has authored dozens of tutorials and articles related to graphics, Web site design, and the Internet, and writes a blog on Web design topics, Brainstorms and Raves. Shirley is also the editor and owner of, a popular and valuable educational resource devoted to website owners, designers, and educators. In addition, Shirley is the author of Deliver First Class Web Sites: 101 Essential Checklists, published by SitePoint Pty. Ltd., July 2006.

Related Optimization Articles, Tutorials, Code Examples

Related within Articles, Tutorials

Related within Web Site Resources's section of annotated links to many top-notch tutorials, tips, recommended books, and more.

top‘On this page’ menuNavigation belowSearch/Sidebar

Articles, Tutorials, Tips:

Most Popular Books

In association with  In Association with 

Our Other Sites


Effective, accessible, standards-based Web site design since 1996

SKDesigns -

Brainstorms & Raves

Weblog on Web design, CSS, standards, music, more. By Shirley Kaiser, SKDesigns.

Brainstorms & Raves

Within Articles, Tutorials, Tips:

Within Web Site Resources:
SkipShow or Hide Detailed Navigation