Thymeleaf: how to use conditionals to dynamically add/remove a CSS class
JavaHtmlCssSpringThymeleafJava Problem Overview
By using Thymeleaf as template engine, is it possible to add/remove dynamically a CSS class to/from a simple div
with the th:if
clause?
Normally, I could use the conditional clause as follows:
<a href="lorem-ipsum.html" th:if="${condition}">Lorem Ipsum</a>
> We will be creating a link to the lorem ipsum page, but only if condition clause is true.
I'm looking for something different: I'd like the block to always visible, but with changeable classes according to the situation.
Java Solutions
Solution 1 - Java
There is also th:classappend
.
<a href="" class="baseclass" th:classappend="${isAdmin} ? adminclass : userclass"></a>
If isAdmin
is true
, then this will result in:
<a href="" class="baseclass adminclass"></a>
Solution 2 - Java
Yes, it is possible to change a CSS class dynamically according to the situation, but not with th:if
. This is done with the elvis operator.
<a href="lorem-ipsum.html" th:class="${isAdmin}? adminclass : userclass">Lorem Ipsum</a>
Solution 3 - Java
For this purpose and if i dont have boolean variable i use the following:
<li th:class="${#strings.contains(content.language,'CZ')} ? active : ''">
Solution 4 - Java
Another very similar answer is to use "equals" instead of "contains".
<li th:class="${#strings.equals(pageTitle,'How It Works')} ? active : ''">
Solution 5 - Java
If you just want to append a class in case of an error you can use th:errorclass="my-error-class"
mentionned in the doc.
<input type="text" th:field="*{datePlanted}" class="small" th:errorclass="fieldError" />
> Applied to a form field tag (input, select, textarea…), it will read the name of the field to be examined from any existing name or th:field attributes in the same tag, and then append the specified CSS class to the tag if such field has any associated errors
Solution 6 - Java
Just to add my own opinion, in case it might be useful to someone. This is what I used.
<div th:class="${request.read ? 'mdl-color-text--grey-800 w500' : ''}"> </div>
Solution 7 - Java
Yet another usage of th:class, same as @NewbLeech and @Charles have posted, but simplified to maximum if there is no "else" case:
<input th:class="${#fields.hasErrors('password')} ? formFieldHasError" />
Does not include class attribute if #fields.hasErrors('password') is false.
Solution 8 - Java
What @Nilsi mentioned is perfectly correct. However, adminclass and user class need to be wrapped in single quotes as this might fail due to Thymeleaf looking for adminClass or userclass variables which should be strings. That said,
it should be: -
<a href="" class="baseclass" th:classappend="${isAdmin} ? 'adminclass' :
'userclass'">
</a>
or just:
<a href="" th:class="${isAdmin} ? 'newclass' :
'baseclass'">
</a>
Solution 9 - Java
If you are looking to add or remove class accordingly if the url contains certain params or not .This is what you can do
<a th:href="@{/admin/home}" th:class="${#httpServletRequest.requestURI.contains('home')} ? 'nav-link active' : 'nav-link'" >
If the url contains 'home' then active class will be added and vice versa.
Solution 10 - Java
Just in case someone is using Bootstrap, I was able to add more than one class:
<a href="" class="baseclass" th:classappend="${isAdmin} ?: 'text-danger font-italic' "></a>