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?

Sal Prima
  • 1,412
  • 2
  • 18
  • 23
Maciaz
  • 1,084
  • 1
  • 16
  • 31
  • *"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. – WebDevBooster Feb 08 '18 at 14:26
  • Possible duplicate of [how add css and js to spring boot application?](https://stackoverflow.com/questions/45536972/how-add-css-and-js-to-spring-boot-application) – riddle_me_this Feb 08 '18 at 14:44
  • Possible duplicate of [Thymeleaf + CSS+SpringBoot](https://stackoverflow.com/questions/41586841/thymeleaf-cssspringboot) – DimaSan Feb 15 '18 at 08: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 – rioV8 Sep 01 '18 at 05:10

1 Answers1

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}"/>
Mahozad
  • 18,032
  • 13
  • 118
  • 133
juanlumn
  • 6,155
  • 2
  • 30
  • 39