如何在 php 中包含 CSS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3483213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I include CSS in php?
提问by Web Worm
i want to include my css/stylesheet via php so that...
我想通过 php 包含我的 css/样式表,以便...
<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
这样我就可以动态更改不同的样式表.... 我该怎么做
采纳答案by Pat
As long as you set your MIME type in style.php
to CSS, you should be in business. Add this to the very top:
只要您将 MIME 类型设置style.php
为 CSS,您就应该开展业务。将此添加到最顶部:
<?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:
如果您在 Apache 服务器上运行,另一种选择是告诉它检查 PHP 的 .css 文件。将此添加到您的.htaccess
文件中以执行此操作:
AddType application/x-httpd-php .css
Then you could simply include a regular .css file:
然后你可以简单地包含一个常规的 .css 文件:
<link rel="stylesheet" href="http://www.mydomain.com/css/style.css">
回答by Mewp
In style.php:
在 style.php 中:
echo file_get_contents('style.css');
This will just output the contents of style.css
.
这将只输出style.css
.
回答by spidEY
Another variation of dynamically changing the page style:
动态更改页面样式的另一种变体:
<link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">
回答by Chris Baaijens
Add multiple CSS files dynamicly
动态添加多个 CSS 文件
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:
他们质疑的答案似乎不同......如果您想在 CSS 映射中添加所有 CSS 文件,并且不必担心在 css 文件名更改或添加另一个文件名时更改代码,请使用:
<?php
foreach(glob("CSS/*.css") as $css_file)
{
echo '<link rel="stylesheet" href="'.$css_file.'" type="text/css" medial="all" />';
}
?>
回答by user387302
why don't you do it the other way around and just include a different css file in your php?
你为什么不反过来做,只在你的 php 中包含一个不同的 css 文件?
print "<link rel='stylesheet' href='$path_to_css_file'>";
回答by Naveed
You can add this php code in your html head section but file should be .php.
你可以在你的 html head 部分添加这个 php 代码,但文件应该是 .php。
For example: index.php
例如: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.
您可以$cssFile
使用不同的条件将任何 css 文件路径存储在变量中。
回答by tamasd
You can directly include CSS into a HTML file:
您可以直接将 CSS 包含到 HTML 文件中:
<style type="text/css">
<?php include 'stylesheet.php'; ?>
</style>