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.  
Filed under: Blog, Tutorials

CSS variables with PHP

Want to have css variables in your css files?
Here’s a simple solution to have variables in your css.
Well, in fact this are PHP variables inside your css files.

Define all the variables that you want in your HTML document like this:

  1.  
  2. <?php
  3. $color1 = ‘#BB180E’;
  4. $color2 = ‘#666666′;
  5. ?>
  6.  

Use them in your CSS file like this:

  1.  
  2. h1 { color:<?php echo $color1 ?>; }
  3. p { color:<?php echo $color2 ?>; }
  4.  

Here you can see an example.

Filed under: Tutorials