首页 > PHP: 无法引用css文件?

PHP: 无法引用css文件?

最近学PHP,到第三章的时候卡住了。我引用了一个HTML模板,但是这个模板无法引用css文件,下图是这个HTML模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><?php echo $page_title; ?></title>    
    <link rel="stylesheet" href="../ch03/includes/style.css" type="text/css" media="screen" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
    <div id="header">
        <h1>Your Website</h1>
        <h2>catchy slogan...</h2>
    </div>
    <div id="navigation">
        <ul>
            <li><a href="index.php">Home Page</a></li>
            <li><a href="calculator.php">Calculator</a></li>
            <li><a href="#">link three</a></li>
            <li><a href="#">link four</a></li>
            <li><a href="#">link five</a></li>
        </ul>
    </div>
    <div id="content"><!-- Start of the page-specific content. -->
<!-- Script 3.2 - header.html -->

但是,我通过在位于表头中的<style>标签里加入:

<?php include ('./includes/style.css'); ?>

它又工作了。通过审查源代码发现所有CSS代码都集中到这个HTML文件来了。我知道这样不好,但有没有办法解决?

还有,我在网站根目录里放入 *.html 文件提示 File not found.(能解析*.php)。

Nginx配置文件:

server {
    listen 80 default_server;
    root /var/www/html;
    index index.html index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

=====================UPDATE on 2016.02.16 00:56===================

这是修改后的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><?php echo $page_title; ?></title>    
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <style>
    <?php include ('./includes/style.css'); ?>
    </style>
    <!-- <link rel="stylesheet" href="../ch03/includes/style.css" type="text/css" media="screen" /> -->
</head>

对,它能工作:

我的项目文件结构:


css那个你看下是不是路径问题

相对路径

我看你模版是两个点,也就是上一级目录

style标签下则是一个点


路径的问题吧,你是在 header.html 里面引入css出问题的?
应该是 ./style.css
或者是../includes/style.css 这样吧


相对路径写错了吧 试试把路径改成你php引用的一样 还有把link那一行往下挪


对了,用Link的方式引用这个路径./includes/style.css 还是不行。


为何要用PHP引用css文件呢?这样做不科学啊


使用绝对路径试试 可以生效再改成相对的


css引用都是用
<link rel="stylesheet" href="../ch03/includes/style.css" type="text/css" media="screen" />
这种形式的 = =,你那种是php的include文件,css不用那种的

【热门文章】
【热门文章】