Dynamic CSS values in Django template

I have a template where there is code like this:

{% with score=some_float_value %}
{% if score > 0 %}
style=“pointer-events: none; background-color: rgba(0, 200, 0, {{score}})”
{% endif %}>
{% endwith %}

And I want styled element opacity to be equal to the current score value. But it doesn’t work unless using a number between 0 and 1 instead of score variable.

Could anyone suggest some solution?

This is expected behavior. RGBA = (red channel, green channel, blue channel, alpha channel).


Take a look… or just read this quote from the page.

The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque)

Thanks for your reply, but a value of the score variable is a number between 0 and 1

Hmmm… my mistake. The examples of {% with %} that I’ve seen use {% with var as value %} format.

I’d try:
{% with score as some_float_value %}

If that doesn’t work, I’d see if the fact that you’re passing a float into a string is the issue. Maybe try:

{% with score as some_float_value|stringformat:".1f" % }


Just used
rgba(0, 200, 0, {{ score|stringformat:".1f" }})
instead of
background-color: rgba(0, 200, 0, {{score}})

Thanks for the idea