我用谷歌搜索了很多,但似乎我做错了什么。
我想做这个:
<?php
include 'header.php';
include'CSS/main.css';
...
?>
但是,我的页面打印了 CSS 代码。
注意:我想使用PHP包含CSS文件,而不是使用 我还想将我的 CSS 文件重命名为 PHP 文件,正如某些网站提到的那样。
有什么线索吗?
非常感谢。
你必须用
<style>
标签包围 CSS:
<?php include 'header.php'; ?>
<style>
<?php include 'CSS/main.css'; ?>
</style>
...
PHP
include
也可以很好地以 .css
结尾。通过这种方式,您甚至可以在 CSS 文件中使用 PHP。这对于组织例如活动确实很有帮助。颜色作为变量。
您将 CSS 代码作为文本包含在 PHP 页面中。为什么不直接以传统方式链接呢?
<link rel="stylesheet" href="CSS/main.css" type="text/css">
您可以使用:
<?php
$css = file_get_contents('CSS/main.css');
echo $css;
?>
并假设 css 文件还没有它,将上面的内容包含在:
<style type="text/css">
...
</style>
要使用“include”包含CSS,您必须告诉PHP您正在使用CSS代码。将其添加到 CSS 文件的标头中,并将其设为 main.php(或 styles.css 或其他):
header("Content-type: text/css; charset: UTF-8");
这可能有助于某些用户的连接,但从理论上讲(阅读:我还没有测试过)会增加服务器的处理器开销,根据 Steve Souder 的说法,因为你的计算机可以一次下载多个文件,使用 include 可以是慢点。如果你把 CSS 分成十几个文件,也许会更快?
史蒂夫的博客文章:http://www.stevesouders.com/blog/2009/04/09/dont-use-import/ 来源:http://css-tricks.com/css-variables-with-php/
<?php
define('CSSPATH', 'template/css/'); //define css path
$cssItem = 'style.css'; //css item to display
?>
<html>
<head>
<title>Including css</title>
<link rel="stylesheet" href="<?php echo (CSSPATH . "$cssItem"); ?>" type="text/css">
</head>
<body>
...
...
</body>
</html>
包含您的 CSS 项目
这是一篇较旧的帖子,但是由于该信息今天仍然相关,因此附加选项可能会对其他人有所帮助。
根据 Stefan 的答案定义文件路径常量。这 定义可以放置在 PHP 页面本身的顶部,或者内部 包含/必需的外部文件,例如 config.php。 (http://php.net/manual/en/function.include.php)
回显 PHP 标签中的常量,然后直接在后面添加文件名。
就是这样!
也适用于其他链接文件,例如 JavaScript。
<?php define('CSS_PATH', 'template/css/'); //define CSS path define('JS_PATH', 'template/js/'); //define JavaScript path ?> <!-- Doctype should be declared, even in PHP file --> <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>main.css"> <script type="text/javascript" src="<?php echo JS_PATH; ?>main.js"></script> </head> <body> </body> </html>
如果您想导入这样的 CSS 文件,只需为文件本身提供 .php 扩展名并导入即可。它会工作得很好:)
您还可以执行以下操作:
将CSS代码文件粘贴到上面创建的文件中
<?php
$minCss=' <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$business = '<link href="bootstrap/css/modern-business.css" rel="stylesheet">';
echo $minCss;
echo $business;
?>
在 html header 中,包含 css 文件,如下
<?php include_once 'includes/bootstrap_css.php'; ?>
你可以这样做
<?php include("Includes/styles.inc"); ?>
然后在这个
include
文件中,有一个指向您的 css 文件的链接。
我不知道你为什么需要这个,但要做到这一点,你可以编辑你的CSS文件:-
<style type="text/css">
body{
...;
...;
}
</style>
您刚刚在此处添加并将其保存为 main.php。您可以继续使用 main.css,但最好使用 .php,因为在您进行编辑后它不会保留为 css 文件
然后像这样编辑你的 HTML 文件。 注意:在标签内添加 include 语句
<html>
<head>
<title>Sample</title>
<?php inculde('css/main.css');>
</head>
<body>
...
...
</body>
</html>
我通过将所有css指令封装在php echo中,然后将其保存为php文件(当然以php标签开始和结束文件),然后包含php文件解决了类似的问题。 这是必要的,因为后面会进行重定向(标头(“somefilename.php”)),并且在重定向之前不允许使用 html 代码。
就放
echo "<link rel='stylesheet' type='text/css' href='CSS/main.css'>";
在 php 代码中,将包含您的样式。为我工作,我尝试过。
这是我有效的格式:
<head>
<title>Site Title</title>
<?php include 'header.php'; ?>
</head>
在我的 header.php 中我有:
<!doctype html>
<html class="no-js" lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/png" href="assets/images/icon/favicon.ico">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
文件名必须是 .CSS 索引以外的名称。写下以下内容:
<link rel="stylesheet" href="style.css" type="text/css" />
我知道已经晚了,但我这样解决了。
<link rel=stylesheet href="/stylesheet.php" type="text/css">
stylesheet.php
的顶部
header("Content-type: text/css; charset: UTF-8");
_跟踪它的目录,我猜
echo css('lib/datatables_rqs/jquery.dataTables.css');
最好的方法是:
第1步: 将 main.css 重命名为 main.php
第2步:在你的main.php中添加
<style> ... </style>
第 3 步:照常添加
<?php include 'main.php'; ?>
我就是这么做的,而且运行顺利..