duanhu2414
2015-03-21 07:26
浏览 107
已采纳

使用php include时不包含CSS文件

I have a css file called main.css in my styles folder on the root of my project:

#header
{
    background-color: aqua;
    height: 120px;
}

In another php file header.php I have something like this:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/styles/main.css"/>
</head>
<body>
<header id="header"></header>

I also have a footer.php:

</body>
</html>

When combining the in my index.php like this:

<?php
    require('includes/header.php');
?>
<?php
    require('includes/footer.php');
?>

the result is that my css rules don't get applied. How can I fix this?

图片转代码服务由CSDN问答提供 功能建议

我的项目根目录中的styles文件夹中有一个名为main.css的css文件:

  #header 
 {
 background-color:aqua; 
 height:120px; 
} 
   
 
 

In 另一个php文件header.php我有这样的事情:

 &lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; link rel =“  stylesheet“href =”/ styles / main.css“/&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; header id =”header“&gt;&lt; / header&gt; 
   
 
 

我还有一个footer.php:

 &lt; / body&gt; 
&lt; / html&gt; 
   
 
 

在我的index.php中组合如下:

 &lt;?php 
 require('includes / header.php')  ; 
?&gt; 
&lt;?php 
 require('includes / footer.php'); 
?&gt; 
   
 
 

结果是 我的css规则没有得到应用。 我该如何解决这个问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushansu9012 2015-03-21 07:33
    已采纳

    problem may be in your css path if php file and style folder both are in root use like this href="styles/main.css"

    点赞 打赏 评论
  • drhwb470572 2015-03-21 07:34

    Link your css file using base

    try

    href="../styles/main.css"
    
    点赞 打赏 评论
  • douba1214 2015-03-21 07:40

    it seems like your css file path cause this problem.

    if your directory structure like this below:

    • includes
    • |__header.php
    • |__footer.php
    • styles
    • |__main.css

    replace the css path in the header.php like:

    href="../styles/main.css"

    点赞 打赏 评论

相关推荐 更多相似问题