6

i want to include my css/stylesheet via php so that...

<link rel="stylesheet" href="http://www.mydomain.com/css/style.php">

so that i can than dynamicly change different stylesheets.... how can i do that

7 Answers 7

7

As long as you set your MIME type in style.php to CSS, you should be in business. Add this to the very top:

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

Another option if you're running on an Apache server is to tell it to check .css files for PHP. Add this to your .htaccess file to do this:

AddType application/x-httpd-php .css 

Then you could simply include a regular .css file:

<link rel="stylesheet" href="http://www.mydomain.com/css/style.css">
Sign up to request clarification or add additional context in comments.

4 Comments

i didnot understant if i have setup all these things then why should i include style.css instead of style.php
Don’t forget to specify the character set.
@testkhan the second example was just showing you how you could get your Apache web server to check .css files for PHP. However setting the correct MIME type in your style.php should allow your original code example to work. I've updated my first example with Gumbo's suggestion of setting the charset at the same time.
Making .css files parsed as PHP doesn't look much of good idea, in my opinion. +1 for the rest of the answer. Spot On.
2

You can add this php code in your html head section but file should be .php.

For example: index.php

<html>
  <head>
   <?php

     $cssFile = "style.css";
     echo "<link rel='stylesheet' href='" . $cssFile . "'>";

   ?>
   </head>
   <body>
    ...
    ...
   </body>
</html>

You can store any css file path in $cssFile variable using different conditions.

Comments

1

In style.php:

echo file_get_contents('style.css');

This will just output the contents of style.css.

Comments

1

Another variation of dynamically changing the page style:

<link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">

Comments

1

Add multiple CSS files dynamicly

The answers seem to be different that they question... If you want to add all CSS files in the CSS map and not have to worry about changing the code whenever a css file name changes or another one is added, use:

 <?php
     foreach(glob("CSS/*.css") as $css_file)
     {
        echo '<link rel="stylesheet" href="'.$css_file.'" type="text/css" medial="all"  />';
     }
 ?>

Comments

0

why don't you do it the other way around and just include a different css file in your php?

print "<link rel='stylesheet' href='$path_to_css_file'>";

Comments

0

You can directly include CSS into a HTML file:

<style type="text/css">
<?php include 'stylesheet.php'; ?>
</style>

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.