CSS没有使用php的相对路径'包括'

时间:2014-11-21 05:27:59

标签: php html css

文件夹结构

 /
 |--index.php

 +--includes
 |--header.html

 +--css
 |--style.css

我的主项目文件夹中有2个子文件夹。一个是名为' includes'的文件夹。而另一个被称为“css'”。 我有我的 - 主文件夹中的index.php文件 - header.html文件夹中的'main/includes' - style.css文件夹中的'main/css'

我的index.php包含header.html,如下所示:include_once('includes/header.html');(本作品!)

我的header.html文件链接了这样的css:<link href='../css/style.css' type='text/css' rel='stylesheet'/> (这不起作用!)

我不明白为什么没有加载css文件 我尝试使用基本标签,虽然我不确定我是否正确使用它。 <base href="http://localhost/main" />(这不起作用)

5 个答案:

答案 0 :(得分:1)

你应该尝试使用

<link href='css/style.css' type='text/css' rel='stylesheet'/>

由于index.phpcss文件夹位于同一级别。

<link href='../css/style.css' type='text/css' rel='stylesheet'/>

您要求您的服务器管理style.css上层index.php中不存在的/

您也可以使用<link href="/css/style.css" type="text/css" rel="stylesheet" /> ,因为它指向网站的文档根目录。

{{1}}

答案 1 :(得分:1)

我认为您不了解include的工作原理。基本上,引用文件中的代码将被复制到主文件中,然后进行处理。因此,如果您将其包含在index.php中,那么您需要相应地引用CSS文件。

以下内容应该有效:

<link href="/css/style.css" type="text/css" rel="stylesheet" />

使用HTML时,您会发现使用绝对路径最容易,即使您将其复制到根目录以外的文件夹中,上述内容仍然有效。 / p>

答案 2 :(得分:1)

$ siteurl =“http:// localhost / project”; (将thi变量存储在配置文件中,以便全局使用)

<link href='../css/style.css' type='text/css' rel='stylesheet'/>

更改为

<link href='<?php echo $siteurl;?>/css/style.css' type='text/css' rel='stylesheet'/>

答案 3 :(得分:0)

只需从<link href='../css/style.css' type='text/css' rel='stylesheet'/>更改为<link href='css/style.css' type='text/css' rel='stylesheet'/>

答案 4 :(得分:0)

如果您尝试使用PHP require在html文件中添加CSS:

<style><?php require("css/style.css");?></style>

注意<style>标签很重要,否则它将回显纯文本

相关问题