Using PHP To Generate CSS

Generating CSS with PHP has several benefits. For example, you can keep all of your colour declarations as PHP variables so if you need to change any colours it only takes a small edit and not a find/replace operation.

Getting PHP to generate CSS requires just two steps. The first thing to do is to open your CSS file and insert the following line at the top. This tells the browser that the file is CSS.

<?php header("Content-type: text/css"); ?>

The next step is to change the file extension on your CSS file from css to php. This will tell the server to parse the file as a PHP script and not a plain CSS file. This is a necessary step unless you have direct access to your web server and can set CSS files to be parsed as PHP, but I don't suggest that you do this as it will slow down the server when it serves normal CSS files. Once you have changed the extension you need to change the link to have the same connection.

<link rel="stylesheet" type="text/css" media="screen" href="style.php">

You are now ready to start putting PHP into your CSS files. Take the following CSS file.

body{
  background:#fff;
  color:#333;
}
h1,h2,h3,h4{
  color:#00840;
}

The first thing we might do is to standardise the colour declarations.

<?php
  header("Content-type: text/css");
  $white = '#fff';
  $dkgray = '#333';
  $dkgreen = '#008400';
?>
body{
  background:<?php echo $white; ?>;
  color:<?php echo $dkgray; ?>;
}
h1,h2,h3,h4{
  color:<?php echo $dkgreen; ?>;
}

A better way to use colours is to call the variable by the function rather than the value of the colour. So using the previous example I will rename the colours so that we use the function.

<?php
  header("Content-type: text/css");
  $background = '#fff';
  $text = '#333';
  $heading = '#008400';
?>
body{
  background:<?php echo $background; ?>;
  color:<?php echo $text; ?>;
}
h1,h2,h3,h4{
  color:<?php echo $heading; ?>;
}

 

Comments

For variable, I will suggest an other approach, using ob_get_contents
Like this:

<?php
ob_start();
?>

color1 __XX__<br>
color2 __XX__<br>
color3 __XX__<br>

<?php for ($x=0; $x<10;$x++){ ?>
data <?php echo "_".$x; ?>__YY__<br>
<?php } ?>

<?php
// Get data from the output buffer
$data = ob_get_contents();

// Set variables
$tab_strtr = array();
$tab_strtr["__XX__"] = "Red";
$tab_strtr["__YY__"] = "Test";
$result = strtr ($data,$tab_strtr);

// Clean buffer then send the result
ob_end_clean ();
echo $result;

?>

 

Permalink

is this a good practice to make css file dynamicaly or use inline styles or internal styles

Permalink

I'm not sure about good practice, but any of these techniques will work. I would highly suggest using caching though to prevent the server having to do any work every time the page is refreshed.

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
13 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.