为什么css不影响我的PHP页面


Why the css is not affecting in my PHP page?

我正在学习php并遇到一些困难,我试图将php文件包含在另一个文件的头中,但css不工作?

这是我的项目目录结构项目结构

和这里是我的PHP代码… blogpage.php

 <html lang="en">
<head>   
    <?php include 'head.php';?>
    <title>Off Canvas Template for Bootstrap</title>
</head>

我已经在Struts-2/blogpage.php中包含了路径

here is my head.php

   <!-- Bootstrap core CSS -->
  <link href="/css/bootstrap.min.css" rel="stylesheet">
  <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">
   <!-- Custom styles for this template -->
  <link href="css/offcanvas.css" rel="stylesheet">
   <link href="css/blog.css" rel="stylesheet">

CSS文件夹位于head.php下吗?

尝试在所有链接中添加斜杠:

   <!-- Bootstrap core CSS -->
  <link href="/css/bootstrap.min.css" rel="stylesheet">
  <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <link href="/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
   <!-- Custom styles for this template -->
  <link href="/css/offcanvas.css" rel="stylesheet">
   <link href="/css/blog.css" rel="stylesheet">

您可以检查,直接添加链接,不包括php。试着用这种方式看看链接是否有效。

解决方案一:

根据您的文件夹结构,您在CSS folder下拥有所有style-sheets,并且您试图将其包含在位于outsideCSS folder的PHP文件中,并且您必须直接将其指向CSS folder以将链接指向您在head.php中提供的所有文件。

head.php应该是这样的,这样就不会出现任何错误。

<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="css/offcanvas.css" rel="stylesheet" type="text/css">
<link href="css/blog.css" rel="stylesheet" type="text/css">

错误:

  1. 在第一个链接中尾随的正斜杠将失败,因为您已将正斜杠添加到bootstrap.css,因此该文件将不会在blogpage.php页面中调用。这就是你没有把css放到blogpage.php的原因。
  2. type="text/css"缺少head.php的样式表调用,这也可能导致错误。

你需要在blogpage.php中调用head.php,如下所示:

<html lang="en">
<head>   
<?php include ('../head.php'); ?>
<title>Off Canvas Template for Bootstrap</title>
</head>

最佳实践

但是你可以把所有的header。php, footer。php, header。php都打包到includes folder中,这样它就会成为那个文件夹下的常量,你可以提供链接而不会出现任何错误

由于你在Struts-2文件夹中调用它,你的路径有时可能会失败,CSS可能会导致错误。

提示:一个网站是完全加载的,你按下CTRL+U,这样你的整个代码将在新的窗口打开(查看页面源),你可以检查是否所有的链接,你正在证明在浏览器中执行正确。

解决方案二:

你可以在CSS文件夹结构上添加正斜杠,因为当你试图将它添加到你使用的任何页面上时,它不会失败。因为在所有调用

时,所有通信都将从根目录完成

你的head.php看起来像

<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="/css/ie10-viewport-bug-workaround.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="/css/offcanvas.css" rel="stylesheet" type="text/css">
<link href="/css/blog.css" rel="stylesheet" type="text/css">