CSS样式表不工作


CSS Stylesheet not Working

我有一个外部CSS样式表,但由于某些原因,图像没有加载。这是我链接到style.css的源代码。

<?
        session_start();
        if(isset($_POST['submit'])){
                if(!isset($_POST['rules'])){
                        $error = "Must read rules at spawn.";
                } else if(isset($_POST['grief'])){
                        $error = "We don't normally allow griefers. Your not allowed on our server unless you ask an Admin.";
                        $_SESSION['banned']=true;
                } else if(!isset($_POST['reg'])){
                        $error = "We prefer regular players.";
                } else if(empty($_POST['username'])){
                        $error = "The username field is empty?.";
                } else if($_POST['dumbtest'] != "6"){
                        $error = "Your too dumb to join this server.";
                } else {
                        include "comms.php";
                        addPlayerToExplorer($_POST['username']);
                        $error = "Welcome to SurviveEquestria, if everything went OK your now a Builder!";
                }
        }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>| Home</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

style.css没有什么特别之处,但它以开头

body {
background-image:url(images/bg.jpg);
}
#container {
margin-left:auto;
margin-right:auto;
width:1000px;
}

我所有的图片都位于图片文件夹中,所以我相信它指向了正确的图片。

记住图像url是相对于css文件路径的,并将路径更改为背景图像:url("images/bg.jpg")

你是否检查了你的路径,这是正确的,或者检查了两个文件在同一位置

由于您评论说"(在我的计算机上)一切正常,但当我将其上传到主机时,它就不再工作了",所以问题显然是在上传过程中(可能以错误的模式传输图像)或服务器端设置中。如果没有任何关于服务器端的相关信息,我们就无法为您提供更多帮助。

试试这个:

background: url(../images/bg.jpg)

让我提供一种方法来调试这个特定问题以及将来的其他CSS参考问题。打开您的页面,然后激活浏览器的开发工具。(Chrome中的CTRL+SHIFT+I)。

转到元素选项卡。导航DOM,直到您看到CSS条目。样式表的URL将是一个可点击的超链接。单击它。查看浏览器导航到的位置。这应该会给你一个关于修复的指示。也许你在文件夹结构中太深了,也许你太浅了。无论如何,我通过这种方式解决了99%的CSS引用问题。