Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use inline CSS by th:style using "The ? : operator" in ThymeLeaf

I am trying to put border color red if a student is fail by using following expression

<div class="image_wrap" th:style="${student.studentExamStatus}?border-style: solid;border-color: red;:">

but I am getting following exception Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "${student.studentExamStatus}?border-style: solid;border-color: red;:"

${student.studentExamStatus} will return either 0 or 1 based student passed exam or not.

like image 985
nand Avatar asked Mar 08 '16 21:03

nand


People also ask

How do you use CSS in 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.

How do you style an inline?

An inline style may be used to apply a unique style for a single element. To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.

How do I include a javascript file in Thymeleaf?

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.


1 Answers

As @Andrew said, you need to need to nest your style statements in single quotes. And you must include your expression inside the brackets :

<div class="image_wrap" th:style="${student.studentExamStatus ? 'border-style: solid;border-color: red;' : ''}">
like image 120
sanluck Avatar answered Sep 26 '22 13:09

sanluck