用thymeleaf设置CSS样式属性

Mus*_*eel 12 css thymeleaf

如何使用百万美分解析的URL设置样式标记的背景属性.

我有

<div style="background:url('<url-to-image>')"></div>
Run Code Online (Sandbox Code Playgroud)

<img th:src="${@/<path-to-image>}">在thymeleaf中是否有相应的设置样式属性.

Lea*_*edo 38

如果您使用th:style设置样式属性,则可以实现此目的:

<div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div>
Run Code Online (Sandbox Code Playgroud)

在thymeleaf论坛上查看主题.

  • 谢谢.语法有时可能是一个真正的头痛. (4认同)

Jar*_*lav 6

@Leandro Carracedo 建议的答案对我不起作用(但它在此过程中有所帮助),我不得不添加第二对括号和 '$' 以从变量中获取值

<td th:style="'font-size:'+@{${headerTemplateTextSize}}+'; -webkit-margin-before: 0.67em; -webkit-margin-after: 0.67em; -webkit-margin-start: 0px;-webkit-margin-end: 0px; font-weight: 300; max-width: 100px'">
    <div>...</div>
</td>
Run Code Online (Sandbox Code Playgroud)


Raf*_*ima 5

您还可以使用文字替换:

<div th:style="|background:url(@{/<path-to-image>});|"></div>
Run Code Online (Sandbox Code Playgroud)