Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS not loading in spring boot application

I have been searching all day, moved the style.css everywhere and still not managed to get it loaded. The images wont load as well.

My structure:

my structure

But if i click the firefox button, it loads:

enter image description here

This is how the style.css is imported in the head of the index:

enter image description here

(tried all kind of combinations)

When i check the developer tools, it says 404 for GET request (style.css and the pictures)

like image 618
I.Stanev Avatar asked Jun 07 '26 12:06

I.Stanev


1 Answers

Spring Boot knows where the static directory is, so you don't need to use the ../ technique to get to the files within. The other part is that you should be using an annotation to get there. It may depend on what view template you are using, but for thymeleaf this is how you would achieve pulling in the css file:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Your Title</title>
    <link rel="stylesheet" th:href="@{/css/style.css}" />
</head>

Notice the annotation in th:href="@{/css/style.css}"

Make sure you include the namespace of your view template, th in the example.

Images work the same way:

<img th:src="@{/img/stanev2.png}" />
like image 100
MAD-HAX Avatar answered Jun 10 '26 04:06

MAD-HAX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!