How do you add a conditional CSS class based on a Python if statement in the following example to show the has-success has-feedback
form element?
<div class="form-group {% if not error_username %} has-success has-feedback {% endif %}">
No, We can not use if-else conditions in CSS as CSS doesn't support logics. But we can use some alternatives to if-else which are discussed below: Method 1: In this method, we will use classes in HTML file to achieve this. We will define different class names according to the conditions which we want to apply in CSS.
No if/else, variables, functions, methods, etc. You cannot use if statements in CSS nor can you in HTML. They're not programming languages, there is no built logic.
Write if condition this way.
<div class="form-group {{'has-success has-feedback' if not error_username }}">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With