3

So, I've spent like last hour trying to solve it, but I just can't get custom .css file to get linked with html files in Spring 5. I'm using Thymeleaf, Bootstrap & jQuery for frontend works. I'm using Intellij Community Edition.

My files hierarchy looks like this:

java
    project_files
resources
    static
        css
          main.css
    templates
        main.html

I have a line in main.html:

<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}" href="../../css/main.css"/>

that should link my styles with the .html file. What can be wrong?

4
  • "What may be wrong?" -- about half a million things "may be wrong". Do you want a list of all of them? I don't think so. Edit your question and post your complete current code snippet there at the bottom by clicking the "JavaScript/HTML/CSS snippet" icon i.e. post the HTML output for the entire page including css links. Commented Feb 8, 2018 at 14:26
  • Possible duplicate of how add css and js to spring boot application? Commented Feb 8, 2018 at 14:44
  • Possible duplicate of Thymeleaf + CSS+SpringBoot Commented Feb 15, 2018 at 8:27
  • Look in your DevToools Network tab what URL is requested, it will be relative to the URL of the page requesting it and is not dependent on the location of your templates Commented Sep 1, 2018 at 5:10

1 Answer 1

6

Try changing a little bit your folder structure, create a folder under resources called static, and a folder called css under static and place the css files there, something like:

🗁 resources
└─── 🗁 static
    └─── 🗁 css
        └─── main.css

Then you should be able to access it by using:

 <link rel="stylesheet" type="text/css" th:href="@{/css/main.css}"/>
Sign up to request clarification or add additional context in comments.

3 Comments

I do it, but give warnmessage and page is not changed by css file. message: o.s.web.servlet.PageNotFound : No mapping for GET /css/style.css
I add that: <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
@Nasta please open a question in order to providde enough information about what is happening

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.