Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include CSS in php?

Tags:

css

php

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

like image 251
Web Worm Avatar asked Dec 29 '22 08:12

Web Worm


2 Answers

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">
like image 86
Pat Avatar answered Dec 31 '22 00:12

Pat


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.

like image 26
Naveed Avatar answered Dec 31 '22 02:12

Naveed