我创建了文件夹结构为www / xyz / images和其他文件夹www / xyz / css。我正在导入css文件和css文件,如下代码。导入css文件时,不显示我的背景图像。
的index.php //
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/mainpage.css" type="text/css">
</head>
<body>
<h1></h1>
</body>
</html>
mainpage.css //
body {
background-image: url("images/foodspot4.jpg");
background-repeat: no-repeat;
background-size:cover;
}
答案 0 :(得分:4)
图像路径必须相对于CSS文件的目录。通过调用images/foodspot4.jpg
,您要求样式表在目录www/xyz/css/images
中查找不存在的文件。
因此,您应该使用相对路径:它应该如下...
body {
background-image: url("../images/foodspot4.jpg");
background-repeat: no-repeat;
background-size:cover;
}
../
将指示CSS在父目录中查找/images/
文件夹。 Here's a quick article explaining how relative file path works.