使用 Flask 的 CSS 背景图像

Par*_*tel 1 html css background-image flask

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

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

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;
}
Run Code Online (Sandbox Code Playgroud)

文件夹

Fri*_*ish 6

CSS 文件被读取为静态文件,因此您不应在 CSS 中使用 url_for 方法。

如果您的文件结构是:

static
  -img
  -css
Run Code Online (Sandbox Code Playgroud)

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

相关SO

  • `url(img/showcase.jpeg)` 不正确,应该是 `url(../img/showcase.jpeg)` (2认同)

小智 6

这是我的目录树结构: 目录树结构

你可以像这样写背景图像:

background-image: url(../img/nodejsEventLoop.png);
Run Code Online (Sandbox Code Playgroud)