How can I include CSS in php?
i want to include my css/stylesheet via php so 开发者_Python百科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
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">
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.
In style.php:
echo file_get_contents('style.css');
This will just output the contents of style.css
.
Another variation of dynamically changing the page style:
<link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">
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" />';
}
?>
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'>";
You can directly include CSS into a HTML file:
<style type="text/css">
<?php include 'stylesheet.php'; ?>
</style>
精彩评论