Search code examples
javacssspringthymeleaf

How do I include my css file, while using Java and Spring with thymeleaf?


I have tried different apporaches. For me, most logical would be the standard way, as I have it now in my "Item.html":

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="${item.name}"></title>
    <link href="styles.css" rel="stylesheet" type="text/css"/>
</head>

This is the form what "drag and drop" in netbeans gives me as a path.

My file structure is shown here:

enter image description here

Any ideas?


Solution

    1. Create a directory static in your src/main/resources/ folder.
    2. Create a directory css in created static folder to store css files.
    3. Put the css files to the css folder.
    4. In html you can include css like <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>

    Here the static folder to serve static contents like css, js files.