How to combine CSS files into one using PHP

Asked By 0 points N/A Posted on -
qa-featured

How to combine your separate CSS files into one using PHP and then compress the CSS with GZIP

SHARE
Best Answer by mohsinaece
Answered By 0 points N/A #110434

How to combine CSS files into one using PHP

qa-featured

Hi,

You can use CSS combining software to allocate many CSS files within one file.

There are some steps to do this.

Gzip will compress the PHP file.

Online lessons are also beneficial to make it understand clearly.

I can present here an address which exhibits all the necessary steps for completing this task.

Thanks.

Best Answer
Best Answer
Answered By 5 points N/A #110436

How to combine CSS files into one using PHP

qa-featured

Hi Mr,

You can use this site for a good quality. I hope this is a much better system. Just go to this site and you will be able to solve your problem. http://electrokami.com/  Copy your code and then to be converted.

Another site this is a not better then previous site: https://www.giftofspeed.com/css-compressor/

Thanks

Answered By 10 points N/A #197349

How to combine CSS files into one using PHP

qa-featured

Hello Jibinjohn,

 With performance in mind I set the following criteria for your solution-

It should cache combined files to increase performance.

When a CSS or JavaScript file is modified the cache should be expired.

It should send a 304 when appropriate.

It should perform more requests a second than requesting the multiple files individually.

It should decrease load on the server.

Here’s an example of a link tag for including multiple CSS files into one request-

<link rel="stylesheet" href="/combine.php?test1&test2" type="text/css">

The combine.php script will infer what files to include from $_GET. In this case it will try to combine a test1.css file and a test2.css file. Let’s have a look at the code I’ve written to meet the first 3 criteria and then we’ll find out if there are any performance benefits.

Most of the big websites have many css files and the browser need to load them one by one.

There is a easy solution to merge them to one file using php.

css_loader.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// First of all send css header
header("Content-type: text/css");

// Array of css files
$css = array(
    'main.css',
    'menu.css',
    'content.css'
);

// Loop the css Array
foreach ($css as $css_file) {

    // Load the content of the css file 
    $css_content = file_get_contents($css_file);

    // print the css content
    echo $css_content;
}
?>

 

1
<link href="css_loader.php" rel="stylesheet" type="text/css" />

Now all the CSS files are merged into one file.

Thanks,

Related Questions