PHP Inside a CSS File, Oh The Possibities!

I love PHP and I love CSS, but until recently I was never easily able to mix the two. Let's say on the home I want a different background than on the other pages. My usual answer was to make two css files and use php if/else statements to call in the specific css file depending on the page. But recently I stumbled on to an interested feature that allows me to use php INSIDE the css file!

Using your .htaccess file, add the following line:

AddType application/x-httpd-php .css

Next, open up your css file (the one you want to include php in) and add this line to the very top, above anything else:

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

Now you can add php directly in your css file which adds the immediate benefit of taking clutter out of your main files, and we all hate clutter!

Enjoy and feel free to ask questions!



Did you enjoy it? If so, please share!
  • StumbleUpon
  • del.icio.us
  • bodytext
  • Technorati
  • Reddit
  • Facebook


Pingback / Trackback:


Post Comment

Name:

Email:

Website:

Comment: