0

I am creating some dynamic template and I have an issue that I want to use PHP variables in external CSS file.

Example: if stylesheet is styles.css and then I want to use this file as styles.php so that I can use variables in this file to make css dynamic.

What should I do for this. Thanks in advance.

4 Answers 4

9
  1. Rename file to styles.php (or configure server to run PHP interpreter in CSS files).
  2. Send appropriate Content-Type header:

    header('Content-Type: text/css');
    
  3. Write your code:

    <?php
    
    header('Content-Type: text/css');
    
    $a = '#123456';
    $b = '#654321';
    
    ?>
    
    body > a {
        color: <?php echo $a ?>;
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Suspect you'll also want to put some "don't cache me" headers in there as well. (Or perhaps I'm just paranoid.) :-)
There is any disadvantage to use server configuration instead header and vice versa.
3

Use an .htaccess file and the following line:

AddType application/x-httpd-php .css

This will make your server parse .css files as if they were .php.

4 Comments

Sounds appropriate but all .css files will be parsed by PHP. If he still uses lots of "legacy" static CSS a slight performance decrease should occur. Anyway +1
thanks for this but i want to know that what should i use. header() in php file or configuration setting in .htaccess file. Which overcomes the time overheads as well as server level issues if any or page loading issue..
@djechelon, Yes - that should be taken into consideration.
A solution I came up with before was to name by static CSS files with a regular .css extension, but the one generated by PHP as *.pcss, then the line in my .htaccess would be AddType application/x-httpd-php .pcss , thus leaving the static CSS files alone.
1

The problem with making dynamically generated CSS files is that they won't be cached. You'll be forcing to user to hit your server at least twice: once for the php output, and once for the css.

If you're only making a few minor parts of the CSS dynamic, consider creating a regular standard CSS file with suitable defaults in it, and then having your PHP pages output a suitable <style> block which issues the overrides. That way your main CSS file can be cached, and you get dynamic styles at the cost of a few lines of extra output in your PHP file.

Comments

-1

I know this is old but @MarcB is incorrect on caching

Here is a cachable version of the php CSS

<?php
  ob_start ("ob_gzhandler");
  header("Content-type: text/css; charset: UTF-8");
  header("Cache-Control: must-revalidate");
  $offset = 60 * 60 ;
  $ExpStr = "Expires: " .
  gmdate("D, d M Y H:i:s",
  time() + $offset) . " GMT";
  header($ExpStr);
  $blue="#00f";
  $red="#f00";
  $green="#0f0";
 ?>
  #div{
   color:<?=$blue?>;
   background:<?=$red?>
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.