我的CSS文件在我的PHP文件中不起作用

时间:2018-06-03 02:56:02

标签: php html css

我无法理解为什么每当我对其进行一些更改时,我的css文件都无法正常工作。我之前输入的代码工作正常,但每当我更改代码时,它都不会显示。

    <?php 
session_start();
 ?>

<?php 

  include "dbt_connect.php";

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Movies&amp;Stuff</title>
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>
  <link rel="stylesheet" href="FontAwesome/css/fontawesome-all.min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="index.css">

  <style>
    #footer{
        width: 100%;
        height: 100px;
        margin-bottom: 0px;
        background-color: gray;
        text-align: center;
        color: white;
    }
    #footer pre{
        padding-top: 50px;
        font-size: 16px;
    }
    #footer a{
        color: white;
        text-decoration: none;
    }
  </style>

  <script type='text/javascript' src='//platform-api.sharethis.com/js/sharethis.js#property=5b0ae82b461c9500119099e0&product=sticky-share-buttons' async='async'></script>
</head>
<body>
    <header id="top_header" style="margin-top: 20px">
        <div class="clear">
        <h1 id="logo">Movies<span id="and">&amp;</span>Stuff</h1>
        <nav id="main_nav">
            <?php 
                if (isset($_SESSION['user_id'])) {
                    echo '
                    <a href="#" class="menu"><i class="fa fa-bars"></i></a>
                    <a href="Movies.php">Movies</a>
            <a href="TV Series.php">TV Series</a>
            <a href="AboutUs.php">About Us</a>
            <a href="ContactUs.php">Contact Us</a>

                    <form action="Includes/logout.inc.php" method="POST" style="float:right; margin-right:1em; border:none;">
                    <button type="submit" name="submit" style="background-color:black; border:none; color: #A5A5A5FF; font-size:17px;outline-style:none;">Logout</button>
                    </form>';
                }else{
                    echo '
                    <a href="#" class="menu"><i class="fa fa-bars"></i></a>
                    <a href="Movies.php">Movies</a>
            <a href="TV Series.php">TV Series</a>
            <a href="AboutUs.php">About Us</a>
            <a href="ContactUs.php">Contact Us</a>
            <span class="login" style="margin-right: -1em;"><a href="login.php">Login</a></span> | <span class="signup"><a href="signup.php">Signup</a></span>';
                }

             ?>

        </nav></div>

这是我的php文件代码,我在菜单中使用字体awesome添加了一个栏,并使其在我的css外部文件中不显示,但它只是显示。我无法理解为什么我的css文件不能处理我的任何其他php文件。我在localhost上运行它。我尝试重新启动但没有找到。

    *{
    margin:0 auto;
}
body{
    font-family: sans-serif;
    background-color: black;
    color:#A8A8A8FF;
}
h1 h2 h3 h4 h5 h6{
    font-weight: 500;
}
.clear{
    clear: both;
}
#top_header{
    height: 150px;
}

.menu{
    display: none;
}
#logo{
    float:left;
    padding: 20px;
    text-transform: uppercase;
    color: white;
}
#and{
    font-weight: normal;
}
#main_nav{
    float: right;
    padding: 30px;


#main_nav a{
    color: #A5A5A5FF;
    margin-right: 1em;
    text-decoration: none;
}
.search{
    display: inline-flex;
    font-size: 20px;
    width: 97%;

}
.searchterm{
    width: 97%;
    font-size: inherit;
    border: 0.1em solid #6D6D6DFF;
    border-radius: 0.5em 0 0 0.5em;
    padding:0.2em 0.5em;
    margin-left: 20px;
    outline-style: none;
    transition: 1s all;
}
.search-btn{
    font-size: inherit;
    border: 0.1em solid #6D6D6DFF;
    border-radius: 0 0.5em 0.5em 0;
    cursor: pointer;
    outline-style: none;
    width: 100px;

}
.search-btn i{
    color: #6B6B6BFF;
}
.searchterm:focus{
    border:0.1em solid #C6C6C6FF;
}

这是我的外部css代码,我在顶部标题下的类菜单中添加了display:none,但我仍然没有得到任何结果。上次我在制作页脚时发生了这个问题,但它没有工作,所以我在内部css中完成了。请帮我理清这件事。

9 个答案:

答案 0 :(得分:3)

所以我遇到了这个问题,我的 CSS 代码运行良好,但是当我删除或添加一些东西时,它并没有在网站上发生变化,我找到了一个非常简单的解决方案,对我有用:

<块引用>

只需按:Ctrl + F5

这被称为“硬刷新”,它在 Windows 和 Chrome 浏览器上对我有用,如果您使用其他东西,请尝试此站点:link

这是一个有点老的问题,但我现在遇到了同样的问题,我想也许我可以帮助某人。

答案 1 :(得分:1)

如果您在另一个文件夹或文件中,请使用单点,切勿使用双点:

<link rel="stylesheet" href="./css/header.css">

答案 2 :(得分:0)

我遇到了类似的问题,我的解决方案是更改您的CSS文件的名称,例如,您之前的CSS文件名为index.css,将其更改为newindex.css,并且不要忘记编辑您的html中的css文件也是如此。我希望它会工作,它为我工作。

答案 3 :(得分:0)

缓存问题。

尝试更改

<link rel="stylesheet" href="index.css">

<link rel="stylesheet" href="index.css?v=<?php echo time(); ?>">

这将强制CSS重新加载。

答案 4 :(得分:0)

问题来自浏览器,只需删除所有浏览数据,历史记录,缓存,Cookie等,然后关闭浏览器并再次打开,它就可以解决此问题,因为它可以解决我的问题。

答案 5 :(得分:0)

在制作第一个Php项目时,我遇到了同样的问题。我使用了相对路径,并通过创建新文件并将该新文件链接到index.php文件中来更改了CSS文件。这对我有用。

 <link href="./styles.css" rel="stylesheet">

答案 6 :(得分:0)

类似于@Varinderjit Kauranswer,我已经将css文件放在具有相对路径的同级目录中(例如../css/styles.css)。客户端浏览器无法访问此目录,因此无法显示。将css/目录移至webroot(或html)目录下而不是旁边的id使得客户端浏览器可以解决该问题。此外,链接然后使用绝对路径而不是相对路径(例如<link rel="stylesheet" type="text/css" href="/css/styles.css">)。

答案 7 :(得分:0)

为我解决了这个问题:
之前:<link rel="stylesheet" href="/css/style.css">
之后:<link rel="stylesheet" href="css/style.css"> //Removed slash before css folder.

答案 8 :(得分:0)

问题:我的浏览器无法访问我修改后的 CSS 文件

原因:我的浏览器已经缓存了这个文件(浏览器已经有了这个文件,他不会再请求了)。

解决方案 1 - 关于开发环境

刷新浏览器的缓存(在 firefox 和 Chrome 上 ctrl+maj+R)。但是您不能在生产中这样做(您无法控制客户端的浏览器)

解决方案 2 - 在生产环境中

更新文件时更改文件的路径。最好的方法似乎是添加如下参数

<link rel="stylesheet" href="index.css?v=1">

并在每次生产修改时更改它

<link rel="stylesheet" href="index.css?v=2">

您可以使用变量来简化多个文件更改的更改。但是请记住,每次修改 CSS 的路径时,所有客户端都会再次请求此资源。例如,不要使用 time() 作为值,否则它会取消您网站客户端的任何缓存(增加服务器费用)。

它将同样应用于 JS 文件和任何其他浏览器可以缓存的资源。