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

时间:2016-10-09 14:42:01

标签: php html css

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

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

这是我的PHP代码... 的 blogpage.php

 <html lang="en">
<head>   
    <?php include 'head.php';?>

    <title>Off Canvas Template for Bootstrap</title>

</head>

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

这是我的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">

2 个答案:

答案 0 :(得分:2)

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。尝试以这种方式查看链接是否有效。

答案 1 :(得分:1)

解决方案一:

根据您的文件夹结构,您拥有style-sheets下的所有CSS folder,并且您正尝试将其包含在位于outside CSS 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. 的原因
  3. type="text/css"中的样式表调用缺少head.php,这也可能导致错误。
  4. 关于这一点,你需要调用head.php中的blogpage.php,如下所示:

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

    最佳实践

    但是你可以将所有head.php,footer.php,header.php包装到includes folder中,这样它就会成为该文件夹下的常量,你可以提供链接而不会出现任何错误。< / p>

    由于您在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">