In this article, we will discuss how to dynamically add and remove CSS class in Thymeleaf templates.
Using th:classappend attribute
Thymeleaf comes with
th:classappend
attribute that was created for adding a style class to an element without overwriting the existing defined classes.
The following example shows how to use it conditionally:
If
${condition}
is true the rendered website will look like the following:
when
${condition}
is false the result will be:
Thymeleaf gives the ability to provide an inline condition without else command, that will look like the following:
In this case when
${condition}
is false, null will be returned and no class will be appended to the attribute.
Comments
Post a Comment