Background images from static folder

303 views
Skip to first unread message

Max Spohn

unread,
Jan 6, 2022, 5:42:39 AM1/6/22
to oTree help & discussion
Hi,

I want to include an image as a background in one of my templates (using e.g. <div style="background-image: url('img.jpg');">). I only found syntax for including an image inside a div (<img src="{% static "my_folder/img.jpg" %}"/>) but I can't figure out the right command to put that image in the background. Could anybody tell me how to do this? Thanks a lot for your help.

Best,
Max

Chris @ oTree

unread,
Jan 6, 2022, 6:33:06 AM1/6/22
to oTree help & discussion
Probably 

url('{{ static 'my_folder/img.jpg' }})

(change {{ to {% if you're using the older oTree 3)

The {{ static }} tag just outputs the URL of the image. its function will make more sense if you try it with a regular image and click 'view source' to see what it output.

Chris @ oTree

unread,
Jan 6, 2022, 6:35:56 AM1/6/22
to oTree help & discussion
Also reading this will explain more how the HTML syntax and oTree tag syntax mix together: https://otree.readthedocs.io/en/latest/templates.html#how-templates-work-an-example
Reply all
Reply to author
Forward
0 new messages