Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set background url for css files in thymeleaf?

I have a thymeleaf template where I don't have CSS files imported and wanted to declare style attribute for the body element with background-image: url{/image.jpg} property with relative image URL. I wanted to load the URL without including the application context name(/myapp/) it. It is similar to the problem over here, except it din't work for me.

CSS:

<style>
body {
    background: url(../img/Background.jpg)
                no-repeat center center fixed;
    background-size: cover;
}
</style>

But the above CSS doesn't work and it accessed the image at

http://localhost/img/Background.jpg. 

So, I had to give the value as url(/myapp/img/Background.jpg) for the image to load properly.

I have the mvc:resources configuration properly set in spring-context.xml for /img/ request to load properly and it works in other places.

spring-context.xml

<mvc:resources mapping="img/**" location="/WEB-INF/img/" />

So how to load the background url image css value dynamically using thymeleaf's relative url?

like image 653
Lucky Avatar asked Mar 04 '16 06:03

Lucky


People also ask

How do I make a background-image my URL in CSS?

Usage is simple — you insert the path to the image you want to include in your page inside the brackets of url() , for example: background-image: url('images/my-image. png'); Note about formatting: The quotes around the URL can be either single or double quotes, and they are optional.

How do you link CSS with Thymeleaf?

Adding CSS. We load the stylesheet using the link tag with Thymeleaf's special th:href attribute. If we've used the expected directory structure, we only need to specify the path below src/main/resources/static. In this case, that's /styles/cssandjs/main.

What is background-image URL?

The url() value allows you to provide a file path to any image, and it will show up as the background for that element. You can also set a data URI for the url() .

How do I add CSS to Thymeleaf in HTML?

The one way to add CSS and JS files is to add them to the static folder of src/main/resources and reference it to the Thymeleaf template. Here @{} is the way of linking URL in Thymeleaf.


2 Answers

So, here's how to set dynamic relative paths in background image url property in the css using thymeleaf's inline text value,

<style th:inline="text">
    body{
        background: url{[[@{/img/Background.jpg}]]}
                    no-repeat center center fixed;
    }
</style>

which loads the image using relative path and we don't have to specific the 'myapp' context name in the url.

like image 135
Lucky Avatar answered Oct 21 '22 00:10

Lucky


In my case that helped: change brackets from curly to round

<style th:inline="text">
body{
    background: url([[@{/img/Background.jpg}]])
                no-repeat center center fixed;
}
</style>
like image 36
Егор Благочиннов Avatar answered Oct 20 '22 23:10

Егор Благочиннов