CSS 带有 Flask 的背景图片

CSS Background Images with Flask

我已经阅读了有关在 Flask 中显示背景图像的其他问题,但是 none 的建议解决方案对我有用,我收到 404 错误(127.0.0.1 - - [22/Feb/2018 15:13:33] "GET /img/showcase.jpeg HTTP/1.1" 404 -)。我要设置为背景的 jpeg 图像位于静态文件夹中,名为 showcase.jpeg。这是我的代码。有什么建议么?这是 HTML Link 到我的 style.css:

<link rel="stylesheet" type="text/css" href="{{url_for('static',filename='style.css')}}">

CSS 文件:

#showcase{
  background-image: url({{ url_for ('static', filename = 'showcase.jpeg') }});
  height: 100vh;
  background-size: cover;
  background-position: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 0 20px;
  font-weight: bold;
}

Folders

CSS 文件被读取为静态文件,因此您不应在 CSS.

中使用 url_for 方法

如果您的文件结构是:

static
  -img
  -css

使用background-image: url(../img/showcase.jpeg)

这是我的目录树结构: directory tree structure

你可以这样写background-image:

background-image: url(../img/nodejsEventLoop.png);

做:

background-image: url('/static/showcase.jpeg');

对我有用。

做:

background-image: url('../images/pic.png');

其中 static->images->pic.png.