Embed PHP Code in CSS files.

Rahul Prasad
A Coder’s Laboratory
1 min readMar 26, 2011

Have you been embedding CSS Styles into PHP files just for the sake of making the styles dynamic? Ever wished to add PHP codes in CSS files itself.

Here is a simple trick which will do the magic for Apache-PHP stack.

Create a .htaccess file in your project directory, preferably inside the directory where CSS files are stored. Open .htaccess file and add following line

AddType application/x-httpd-php .css

Now you can include php codes in CSS files. Here is a small example

#container {
background: <?php echo $userColour; ?>;
}

--

--