gpt4 book ai didi

css - .css 文件中的 Thymeleaf URL 表达式

转载 作者:行者123 更新时间:2023-11-28 09:16:47 36 4
gpt4 key购买 nike

我正在使用 Spring MVC 和 Thymeleaf View 引擎。我有一个外部 style.css 文件,其中包含我页面的 CSS。在 CSS 文件中,我想引用一个相对的图像 URL。

这是我的style.css 文件:

.background {
width: 100%;
background-image: url('/path/to/image/bg.png');
}

以上尝试访问具有以下 URL 的图像,该 URL 不存在:

http://localhost/path/to/image/bg.png

我的真实形象在:

http://localhost/myapp/path/to/image/bg.png

这个 StackOverflow 问题给出了我的内联 CSS 问题的答案:How to set background url for css files in thymeleaf?

但是我如何为外部 .css 文件执行此操作?

最佳答案

将所有带有图像 path/to/image/bg.png 的图像文件夹结构放入 resources/static 下的 images 文件夹中。然后在 CSS 中将其引用为:

.background {
width: 100%;
background-image: url('../images/path/to/image/bg.png');
}

然后您可以在您的模板中使用此 CSS 作为:

<head>
<link rel="stylesheet" media="screen" th:href="@{/css/application.css}"/>
</head>

你的文件结构应该是这样的:

enter image description here

关于css - .css 文件中的 Thymeleaf URL 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105511/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com