我有一个索引PHP页面,其中包括所有PHP文件,如index.php?page=example
。所有页面都在另一个文件夹中,结构如下:
public_html/index.php
public_html/css/style.php
public_html/pages/
索引从css/style.php
调用CSS文件。
从index.php
(include pages/example.php
)使用GET函数调用页面。
如果我运行index.php
,我没有CSS问题,如果我只运行example.php
这样的包含页面,我得到CSS问题,因为CSS是在index.php
,显然不会显示CSS正确。
但是当我运行index.php
并包含index.php?page=example
时,索引CSS显示正确,但包含的页面中的类不起作用…
我想包含只会导入代码,但它似乎是错误的服务器或我做错了什么?
这里是我正在使用的示例代码。这是index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php
include('pages/example.php');
?>
</body>
</html>
Index.php所有的css类工作正常,但从包含的页面的样式类不工作,他们只是没有样式
你不应该在php文件中编写css代码。最好创建一个css文件,把你的样式指令放在里面。您可以通过以下约定最好地包含css样式,创建一个基本的html模板,如下所示,并链接到您的css文件,并将php包含在其中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="part/to/file.css"> <!-- link your stylesheet here -->
</head>
<body>
<?php
include('path/to/file.php'); // include your php code here
?>
</body>
</html>
确保你有header("Content-type: text/css");作为PHP文件的第一行,使其正确呈现为css。然后不包含该文件。相反,像普通css文件一样引用它,只是将。css更改为。php。<link rel="stylesheet" href="part/to/file.php">
。这应该能让你工作起来。我假设您从数据库中提取数据来填充css,因此请确保格式正确。不要使用.headertext{
color:<?=$row['headercolor'];?>;
之类的东西。而是在php标签中声明。$color= $row['headercolor'];
。然后在php文件的css部分调用该变量。.headertext{
color:<?=$headercolor?>;
。希望对大家有所帮助