繁体   English   中英

未应用网站样式。 (从 php 包含调用 stylesheet.css)

[英]Website styling not being applied. (Calling stylesheet.css from a php include)

我想知道是否有人可以帮助我解决我在第一次使用 HTML、CSS 和 PHP 创建网站时遇到的一些问题。 (我之前在网页设计方面的尝试仅使用 HTML 和 CSS)。

目前的问题是我的主页( index.php )不知何故没有“看到”我的stylesheet.css

index.php的代码基本上如下:

    <?php
      $page_title='Home';
      [php-code here, to call in include1.php.....Please see below for details]
    ?>
    <div class="copy">
      [page content here, in html]
    </div>
   <?php
      [php-code here, to call in include2.php.....Please see below for details]
?>

我的文件夹结构是:web

   css
     stylesheet.css
   images
     logo.png
   includes
     include1.php
     include2.php
   index.php

在尝试调用include1.php (包含 doc 类型声明和 Head 部分,包括对stylesheet.css的引用)时,我尝试了以下操作(插入<?php?>之间,如上所示),但都没有成功:

$pathtoinclude1 = $_SERVER]'DOCUMENT_ROOT'];
$pathtoinclude1 .= "/includes/include1.php";
include_once($pathtoinclude1);

include('/includes/include1.php')

include1.php ,我对stylesheet.css的引用是:

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

当我预览主页时,我得到的只是默认字体的文本(Times New Roman?)。 没有应用任何通过 CSS 设置的样式。

谁能给我一些关于我做错了什么的指示?

如果第一个答案不起作用,请尝试更换

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

使用<?php include 'style.php'?>

然后在 style.php 文件中包含<style>标签,然后定期添加 css。

  1. 既然你说你是第一次使用php ,请确保你有正确的html声明。

尽管您已经使用过html,css

<?php
      $page_title='Home';
      [php-code here, to call in include1.php.....Please see below for details]
?>
<!DOCTYPE html>
<html>
<head>
 <!-- MAKE SURE YOU'RE INCLUDING THE
  EXTERNAL CSS FILE HERE BUT NOT TO INCLUDE YOUR PHP INCLUDES!
  WHICH ACCORDING TO YOUR FILE STRUCTURE SHOULD BE -->
<link rel="stylesheet" 
      href="../css/stylesheet.css" 
      media="Screen" 
      type="text/css"/>
</head>
<body>
<div class="copy">
      [page content here, in html]
    </div>
   <?php
      [php-code here, to call in include2.php.....Please see below for details]
?>
</body>
</html>

当您include()来自index.php的文件时,路径如下所示:

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

此路径从index.php返回一个目录,这不是有效路径。 当您将它包含在index.php中时,您在include1.php中的路径应该如下所示:

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

此外,如果 CSS 正确包含但样式仍未显示,请尝试删除浏览器缓存。

试试这个。 它对我有用

假设您的 CSS 文件名为“css.css”

它与您的主页位于同一目录中。

只需在 head 标签中添加:

Head
Style
<?php include('css.css') ?>
style
head

不要忘记添加相应的标签

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM