我需要在电子邮件模板 (thymeleaf) 中插入样式。我需要为几个模板制作一个通用的 css 文件。
使用spring boot 1.3.3
我试着按照以下帖子中的内容进行操作: Thymeleaf + CSS+SpringBoot
电子邮件文件:资源 > 邮件 > "email.html" css 文件:resources > static > css > "styles.css"
我为邮件使用 WebContext(我曾经使用 Context,但 Context 不接受相对路径,因此需要 WebContext)。
在我的CSS中:
h1 {
color: #78ab46;
}
在我的模板中:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title > Hello </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../static/css/styles.css" th:href="@{/css/styles.css}" />
</head>
<body>
<h1> Hello </h1>
</body>
结果是邮件发送了但是没有考虑css样式。我也没有看到任何错误消息。
你能帮帮我吗?
谢谢,
曼努埃拉
最佳答案
过去我使用片段来处理这种事情。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<div th:replace="fragments/email-header :: head"></div>
<title > Hello </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1> Hello </h1>
</body>
然后在片段文件夹中我有一个文件 email-header.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<style>
h1 {
color: #78ab46;
}
</style>
</html>
在我复制它的元素中,我使用了整个标题的片段。我不明白为什么它不适用于 header 的“片段”。
关于css - 在 thymeleaf 模板中插入 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57474988/