blog icon

PHP variables inside external CSS

There’s a way of using php variables inside your css external file.
First, you have to rename your css file to “mycssfile.php”.
Then, insert the following line of code on top of the file:

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

Finally, don’t forget that the reference to the css file in your HTML is “mycssfile.php”.

Inside “mycssfile.php” you can define variables like colors, typography, measures, etc…

  1.  
  2. <?php
  3. $color1 = "#CCCCCC";
  4. $color2 = "#FFFFFF";
  5. $type1 = "Helvetica";
  6. etc…
  7. ?>
  8.  

Then, use them with a normal echo statement:

  1.  
  2. h1 {
  3. color: <?php echo $color1 ?>;
  4. font-family: <?php echo $type1 ?>;
  5. }
  6.  
Filed under: Blog, Tutorials

Comments are closed.