简体   繁体   English

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

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

I wonder whether anyone could help me resolve some problems I'm having in creating a website using HTML, CSS...and PHP for the first time.我想知道是否有人可以帮助我解决我在第一次使用 HTML、CSS 和 PHP 创建网站时遇到的一些问题。 (My previous attempts at web design were only in HTML and CSS). (我之前在网页设计方面的尝试仅使用 HTML 和 CSS)。

The problem at present is that my home-page ( index.php ) somehow isn't 'seeing' my stylesheet.css .目前的问题是我的主页( index.php )不知何故没有“看到”我的stylesheet.css

The code for the index.php is basically as follows: 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]
?>

My folder structure is: web我的文件夹结构是:web

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

In attempting to call in include1.php (containing doc type declaration, and Head section including reference to stylesheet.css ), I've tried the following (inserted between <?php and ?> , as shown above), all without success:在尝试调用include1.php (包含 doc 类型声明和 Head 部分,包括对stylesheet.css的引用)时,我尝试了以下操作(插入<?php?>之间,如上所示),但都没有成功:

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

and

include('/includes/include1.php')

In include1.php , my reference to the stylesheet.css is:include1.php ,我对stylesheet.css的引用是:

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

When I preview the home-page, all I get is the text in default font (Times New Roman?).当我预览主页时,我得到的只是默认字体的文本(Times New Roman?)。 None of the styling via CSS is being applied.没有应用任何通过 CSS 设置的样式。

Can anyone give me some pointers as to what I'm doing wrong?谁能给我一些关于我做错了什么的指示?

If that first answer doesn't work, try replacing如果第一个答案不起作用,请尝试更换

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

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

And then in the style.php file include the <style> tags and then add the css regularly.然后在 style.php 文件中包含<style>标签,然后定期添加 css。

  1. Since you say you are using php for the first time, make sure you have the correct html declaration.既然你说你是第一次使用php ,请确保你有正确的html声明。

although you have already worked with html,css just a reminder:尽管您已经使用过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>

When you include() the file from index.php , the path looks like:当您include()来自index.php的文件时,路径如下所示:

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

This path is going one directory back from index.php which is not a valid path.此路径从index.php返回一个目录,这不是有效路径。 Your path in include1.php should look like this when you include it in index.php :当您将它包含在index.php中时,您在include1.php中的路径应该如下所示:

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

Also, if CSS includes properly but styles still do not show up, try removing browser cache.此外,如果 CSS 正确包含但样式仍未显示,请尝试删除浏览器缓存。

Try this.试试这个。 It worked for me它对我有用

Let's assume that you CSS file is named 'css.css'假设您的 CSS 文件名为“css.css”

And it is located in the same directory with you home page.它与您的主页位于同一目录中。

Just add this at the head tag:只需在 head 标签中添加:

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

Don't forget to add corresponding tags不要忘记添加相应的标签

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

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