RSS
 

Embed PHP Code in CSS files.

26 Mar

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.

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; ?>;
}

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

 
 

Leave a Reply

 

CommentLuv Enabled
 
  1. nfo

    September 21, 2011 at 9:03 PM

    don t get me wrong, but i would recommend to separate code and design…

     
  2. Rahul

    September 22, 2011 at 6:37 PM

    I agree, its a bad practice.
    Anyways, with LESS you can do programming in CSS too.
    Rahul´s last blog ..Embed PHP Code in CSS files.My ComLuv Profile