Dynamic CSS using PHP

Dynamic CSS is the perfect way to create themes and allow themes to be easily implemented. We found the following snippet doing it's rounds on Twitter and felt it is worthy of a blog post.
With some minor editing you can give the option to create themes for your framework relatively fast and easily.

 

Create a new PHP file, and add the following to the very beginning of the page:


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

 

Underneath that, you can place your normal CSS. In addition, you can add PHP to it as well!

For example:


<?php
header("Content-type: text/css");
session_start();
if(isset($_SESSION['user'])){
echo '
.home{font-size:10px;}
';
}else{
echo '
.home{font-size:30px;}
';
}
?>


Once you have your CSS created, and saved, you can then add and link to it like so:
<link rel="stylesheet" type="text/css" href="themes/default.php">

 

SOURCE: http://phpsnips.com/66/Dynamic-CSS-Style-Sheet

August 03 2016 - 10:51:05

Category: Code snippets

Share