我有一个晶闸管模板,在那里我没有导入CSS文件,并且希望用相对图像URL来声明具有background-image: url{/image.jpg}属性的body元素的样式属性。我想加载URL,而不包括应用程序上下文名称(/myapp/)。它类似于这里上的问题,只是它不适合我。
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中为/img/请求正确设置了/img/配置,以便正确加载,并且在其他地方工作。
spring-context.xml
<mvc:resources mapping="img/**" location="/WEB-INF/img/" />那么,如何使用晶闸管的相对url动态加载背景url图像的css值?
发布于 2016-03-04 06:36:19
因此,下面是如何在css中设置背景图像url属性中的动态相对路径,
<style th:inline="text">
body{
background: url{[[@{/img/Background.jpg}]]}
no-repeat center center fixed;
}
</style>它使用相对路径加载图像,我们不必在url中指定“myapp”上下文名称。
发布于 2018-11-02 16:27:59
在我的例子中,这是有帮助的:将括号从卷改为圆。
<style th:inline="text">
body{
background: url([[@{/img/Background.jpg}]])
no-repeat center center fixed;
}
</style>发布于 2019-04-16 23:19:06
另一种选择是:
<body th:style="'background: url(/img/la-paz-city.jpg) no-repeat center center fixed;'">
...
</body>仅此而已
https://stackoverflow.com/questions/35789526
复制相似问题