[英]How to set background url for css files in thymeleaf?
我有一個 thymeleaf 模板,我沒有導入 CSS 文件,我想用background-image: url{/image.jpg}
屬性和相對圖像 URL 聲明 body 元素的 style 屬性。 我想在不包含應用程序上下文名稱( /myapp/
)的情況下加載 URL。 它類似於這里的問題,只是它對我不起作用。
CSS:
<style>
body {
background: url(../img/Background.jpg)
no-repeat center center fixed;
background-size: cover;
}
</style>
但是上面的 CSS 不起作用,它訪問了圖像
http://localhost/img/Background.jpg.
因此,我必須將值指定為url(/myapp/img/Background.jpg)
才能正確加載圖像。
我在spring-context.xml
正確設置了mvc:resources
配置,以便/img/
請求正確加載,並且它可以在其他地方工作。
spring-context.xml
<mvc:resources mapping="img/**" location="/WEB-INF/img/" />
那么如何使用 thymeleaf 的相對 url 動態加載背景 url 圖片 css 值呢?
所以,這里是如何使用 thymeleaf 的內聯文本值在 css 中的背景圖像 url 屬性中設置動態相對路徑,
<style th:inline="text">
body{
background: url{[[@{/img/Background.jpg}]]}
no-repeat center center fixed;
}
</style>
它使用相對路徑加載圖像,我們不必在 url 中指定“myapp”上下文名稱。
另一種選擇是:
<body th:style="'background: url(/img/la-paz-city.jpg) no-repeat center center fixed;'">
...
</body>
就是這樣😉
在我的情況下有幫助:將括號從卷曲更改為圓形
<style th:inline="text">
body{
background: url([[@{/img/Background.jpg}]])
no-repeat center center fixed;
}
</style>
<body th:style="'background-image:url(' + @{/images/background.jpg} + '); background-repeat: no-repeat, repeat; background-size: cover;'">
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.