简体   繁体   中英

Insert css in thymeleaf template

I need to insert styles in email template (thymeleaf). I need to make a common css files for several templates.

Using spring boot 1.3.3

I tried to follow what's in the following post: Thymeleaf + CSS+SpringBoot

email file: resources > mails > "email.html" css file: resources > static > css > "styles.css"

I use a WebContext for the mail (I used to use a Context but the relative path was not accepted with Context and WebContext was required).

In my css:

h1 {
color: #78ab46;
}

In my template:

<!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>

The result is that the mail is sent but the css style is not taken into an account. I don't see any error message either.

Could you help me, please?

Thanks,

Manuela

In the past I've made use of fragments to handle this type of thing.

<!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>

Then in the fragments folder I have a file email-header.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <style>
        h1 {
            color: #78ab46;
        }
    </style>
</html>

In the project I copied this out of, I used the fragment for the entire header. I dont see why it wouldn't work for a "fragment" of the header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM