Thymeleaf: how to use conditionals to add/remove dynamically a CSS class


Tags: html,css,thymeleaf

Problem :

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 a block always visible, but with changeable classes according to the situation.



Solution :

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>

    CSS Howto..

    How to add opacity on background?

    Avoid children to interfer with the parents hover. (or how to .stop(false,true) a css transition)

    How do I add a CSS3 transition to an image overlay?

    How to visually identify the viewbox of a SVG with CSS?

    Bootstrap CSS How To: Create a ContentBox with a Title Bar

    How do I center this menu?

    How to use icon images?

    How do I implement this basic CSS for just one category?

    How to set my span text vertical align and next to my image?

    How to fix the top row and first column in a webpage, with only CSS

    How can I place an element at the top of a page using CSS?

    How to divide parent div's height between child divs

    How to get slanted borders in CSS

    Javascript slideshow that gradually zooms on image [closed]

    How can I get these two form fields to fill the space between them [duplicate]

    how to properly override CSS in external file

    How can i override css property

    How to make my asp chart scrollable in the x direction

    User agent, how to have diferent css files for diferent devices automaticly

    Force jQuery show/hide to respect display: table?

    how to add empty space between cells in tables?

    SMACSS and BEM: How to position Module inside of a Module?

    How to change position of a sphere overtime using css

    how to make cross browser css ellipsis?

    How to start css and html

    how to revert to original css values after .css() method

    How to align an element when it locates below some float elements?

    How to download CSS file through JQuery

    how to programmatically call the “click” of css

    How to Implement YUI Compresser in a website [closed]