Compress Multiple CSS Files

Compress multiple CSS files

Web site optimization geeks suggest that we should try to minimize the size of files while serving web pages. Most of the time, web designers use multiple CSS files for make task management easier, but this requires as many HTTP requests as there are CSS files. Following script will allow us to serve all your CSS files as a single HTTP resource, minified (by removing comments and extraneous white space), and gzip-compressed.

 

header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
  return $buffer;
}

/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');

ob_end_flush();

 

Enjoy!

Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!

Tags:  

Share It

Related Posts