How can I apply style to a div based on condition in thymeleaf?

前端 未结 2 1643
灰色年华
灰色年华 2021-01-11 23:26

I have a

block which I need to set to display:none or display:block based on the condition. The html looks like this,

相关标签:
2条回答
  • 2021-01-11 23:43

    Solved it while posting the question,

    th:style="${condition ? 'display:block' : 'display:none'}" >
    

    would produce the necessary conditional style. If condition is true display is set to block and none if condition is false.

    For admin,

    th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >
    

    the style is set to display:block and for other roles the block is not displayed.

    0 讨论(0)
  • 2021-01-12 00:02

    In simple case it could be written as

    <div th:style="${filed==null} ? 'opacity:.3'">
    
    0 讨论(0)
提交回复
热议问题