html - CSS 背景图片不会加载 Django 2.1.2

标签 html css django-staticfiles django-settings

我正在寻求有关此问题的帮助。我已经研究到死了,我对 django 比较陌生,而且我碰壁了。我的背景图片不会从我的 CSS 文件加载。我尝试了不同的目录格式 img/image.jpg static/image.jpg 等。在过去的三个小时里,我已经阅读了尽可能多的以前的 stackoverflow 帖子以及各种教程,但不幸的是没有解决这个问题。任何帮助将不胜感激,并在此先感谢您:)

图片目录为:static/img/forrest2.jpg

CSS目录:static/css/style.css

Error Codes:
"GET / HTTP/1.1" 200 2074
[24/Nov/2018 14:10:51] "GET /static/css/style.css HTTP/1.1" 304 0
[24/Nov/2018 14:13:28] "GET / HTTP/1.1" 200 2074
[24/Nov/2018 14:13:29] "GET /static/css/style.css HTTP/1.1" 200 1234
[24/Nov/2018 14:14:29] "GET / HTTP/1.1" 200 2074
[24/Nov/2018 14:14:29] "GET /static/css/style.css HTTP/1.1" 200 1235



CSS File:
@import url('https://fonts.googleapis.com/css?family=Open+Sans:800|Poppins:500');


html, body{
margin: 0;
padding:0;
height: 100%;
width: 100%;
}

.intro {
height: 100%;
width: 100%;
margin-auto: auto;
background-image: url('/static/img/forrest2.jpg') no-repeat 50% 50%;
background-size: cover;
/*Allows the whole image to display*/
display: table;
top: 0;
/*no top white line*/
}


Django Settings in settings.py:

STATIC_URL = '/static/'
STATIC_DIRS = 'static'
STATICFILES_DIRS = [
    STATIC_DIRS,
]

最佳答案

我认为问题在于您正在使用 background-image 属性为背景应用所有样式,但是 that property仅用于显示图像本身以及您想要伴随它的任何渐变。

你想要的是shorthand property这允许您在一行。

只需将 background-image 替换为 background 即可。

关于html - CSS 背景图片不会加载 Django 2.1.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459205/

相关文章:

html - 更好的地方来存储您的网站图标文件?

javascript - 将 ul 作为输入

css - 如何在我网站上的 tumblr 帖子中导入我的 tumblr 的 css?

python - Django - 无法加载保存到数据库的图像

python - Django 静态文件未在 Azure 上提供

javascript - 停止一个 div 的背景颜色覆盖另一个 div 的内容,旋转并因此重叠,div

javascript - 未捕获的类型错误 : Cannot read property 'top' of undefined . offset().top-500;

jquery - 使我的网站的某些部分更具移动性和不同的屏幕分辨率大小友好

jquery - 使用 jQuery .hover 函数

css - STATIC_ROOT 找不到我的静态文件