How to set background image in Django template?

How to set background image in Django template? Let's say the address of my background image is at www.randomlink.com/static/backgroundimagesplash.jpg

I looked at the Pinterest homepage. As many of you know, Pinterest was built using Django. I checked a few of their elements on my homepage.

One of the graphic elements they used was here: PinterestSplashImage.jpg

The burst image is set in the center from the bottom. One thing I noticed is that the splash image changes relative to the size of the browser in which it is viewed.

This is my current homepage template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <link href="www.randomlink.com/static/favicon.ico" rel="icon" type="image/x-icon"> <head> <title>Homepage</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <style> body{ font-family:Arial,Helvetica,sans-serif; font-size: 12px; } </style> </head> <body> {{ state }} <form action="/login/" method="post"> {% if next %} <input type="hidden" name="next" value="{{ next }}" /> {% endif %} username: <input type="text" name="username" value="{{ username}}" /><br /> password: <input type="password" name="password" value="" /><br /> <input type="submit" value="Log In" /> </form> </body> </html> 

How can I set www.randomlink.com/static/backgroundimagesplash.jpg in the lower center and make it accessible, as Pinterest did with its homepage? (For the sake of argument, the splash will be the same size as the Pinterest popup)

+4
source share
3 answers

See how I did it: in the template I put the following line:

 <body id="bg" style="background-image: url('{% static "images/33.jpg"%}')";> 

And in css:

 #bg{ background-size: 1800px 900px; background-repeat: no-repeat; background-position: top; background-attachment: fixed; } 

As a result, I got a fixed background and screen proportions.

+2
source

This question does not apply to Django. This is pure html with CSS.

To scale the image while resizing, you must use the background-size: contain property for CSS3.

To place bottom and center, you can use position: absolute; background-position: 50% 100%; position: absolute; background-position: 50% 100%; for CSS.

Finally, an example:

 <style type="text/css"> .splash { bottom: 0; left: 0; right: 0; background-image: url('https://s-passets-ec.pinimg.com/webapp/app/desktop/images/logged_out_splash.77aa6339.jpg?1373235165'); background-size: contain; background-position: 50% 100%; background-repeat: no-repeat; bottom: 0; height: 100%; margin: auto; max-height: 461px; max-width: 1034px; position: absolute; width: 100%; } </style> <div class="splash"></div> 

see demo on jsfiddle

0
source

Try setting the background image in the template:

  <div class="inner" style="background-image: url({% static "frontend/img/parallax-image.jpg" %}");> 
0
source

Source: https://habr.com/ru/post/1490787/


All Articles