How to put an image as a background in HTML/CSS - html

I encountered a problem while I tried to add an image to my blog. I was trying to put my image as a background and it did not work. How do I solve this.I also want it to cover the entirety of my blog's body.
{% extends 'base.html' %}
{% block content %}
<style>
body {
background-image: url('https://media.vanguardcommunications.net/blog-e1505840253663.jpg" alt="A blog" class="main-img');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
}
</style>
<h1>Posts</h1>
<ul>
{% for post in object_list %}
<li>{{post.title}} - {{post.author.first_name}} {{post.author.last_name}} -
{{ post.post_date }} <small>
{% if user.is_authenticated %}
- (Edit)
(Delete)
{% endif %}
</small><br/>
{{post.body|slice:":200" }}</li>
{% endfor %}
</ul>
{% endblock %}

In your background image url you mistakenly pasted the img tag url alongside the class and alt attribute just replace
background-image: url('https://media.vanguardcommunications.net/blog-e1505840253663.jpg" alt="A blog" class="main-img');
with this
background-image: url('https://media.vanguardcommunications.net/blog-e1505840253663.jpg');

You can use these styles
body {
background-image: url('https://media.vanguardcommunications.net/bloge1505840253663.jpg');
background-size: cover;
background-position: center;
background-attachment: fixed;
height: 100vh;
width: 100%;
}

Related

css only solution to hide element when page content is less than or equal to 110vh or an equivalent solution

I have implemented a css only back to top button, but on shorter pages it shows when it is not needed.
.top {
position: sticky;
bottom: 9px;
padding: 9px;
place-self: end;
margin-top: 109vh;
font-weight:700;
border-radius: 9px;
color: var(--a1);
background: var(--c2);
}
.top:hover {
color: var(--c2);
background: var(--a1);
text-decoration: none;
}
You can see it in the bottom left corner here: https://abridge.netlify.app/overview-abridge/ (don't need to see it when the page is this short)
It works fine so long as its a longer page: https://abridge.netlify.app/overview-code-blocks/
I have thought of artificially increasing the size of short pages, but that just seems kinda hacky, also I know I can easily resolve this with javascript, but I am trying to find a solution that does not rely on javascript. I tried playing around with media queries but could not find any that actually query how much content is in a viewport.
If you are familiar with zola, the repo is here: https://github.com/Jieiku/abridge
If you have zola installed you can clone the repository and run zola serve from the directory, to test changes locally.
Here is the file with the back to top: https://github.com/Jieiku/abridge/blob/master/sass/include/_top.scss
EDIT: for the moment I have discovered a creative way of resolving this, because its a SSG and zola has readtime value I did this:
{%- block gotop %}
{%- if page.reading_time %}
{%- if page.reading_time > 1 %}
&cuwed;
{%- endif %}
{%- endif %}
{%- endblock gotop %}
The only thing to resolve now is to somehow get it over on the right side of the page instead of the left side.
This is the solution I used:
.topout {
position: sticky;
bottom: 1px;
padding: 20px;
place-self: end;
margin-top: 110vh;
pointer-events: none;
}
.topleft {
margin-left: calc(100% - 80px);
}
.top {
pointer-events: all;
padding: 9px;
border-radius: 9px;
font-weight:700;
color: #FF9900;
background: #222222;
}
.top:hover {
text-decoration: none;
color: #222222;
background: #FF9900;
}
Then in Zola Template page:
{%- block gotop %}
{%- if page.reading_time %}
{%- if page.reading_time > 1 %}
<span class="topout">
<span class="topleft"> </span>&cuwed;
</span>
{%- endif %}
{%- endif %}
{%- endblock gotop %}
You can see it here: https://abridge.netlify.app/overview-code-blocks/
I would have liked to avoid using calc() however this does work without issue in: Firefox, Chrome, Android Chrome
I think the cleanest solution would be to utilize #media screen and (max-height: 110vh)
Here is my suggestion:
#media screen and (max-height: 110vh){
.top{
display:none;
}
}
The idea here is that if the page's viewport is currently within that max-height of 110vh then we want to set .top to no longer display on the page.

How to add background image through css in django?

How to get the path to work so it shows the background image through css?
If it could be done with a relative path then how?
right now with this code, the background image is not being displayed.
Thanks for any help.
/*mobile view*/
.index-banner {
background: url('static/mysite/images/home.svg') no-repeat;
background-position: center;
background-size: cover;
width: 100%;
height: 380px;
}
{% block content %}
<section class="index-banner">
<h2>Test<br>Condiitons</h2>
<h1>lorel ipsum</h1>
<h1>sdfjls upueh ndsfoi bbownl</h1>
<img src="{% static 'mysite/images/home.svg' %}" class="mysite-img" style="display: none;">
</section>
{% endblock %}
figured out the way,
so if you have your settings.py set to STATIC_URL = '/static/'
then background: url('/static/movies/images/home.svg') no-repeat;
should work,
what I was missing was a pre'/' before the static

How can I add animations to my images in my Django app?

I´m trying to add a kind of animation to some images in my Django app, what I want to do is that when the user moves the mouse around the image it gets bigger.
I tried adding some code in my CSS but the image won't change
Thank you for your help.
My index.html
{%block contenido %}
<div id="container" class="foto_pelicula">
{% for p in peliculas %}
{% if p.genero.id == 1 %}
<img src={{p.urlPortada}} width="289" height="289"/></li>
{% endif %}
{% endfor %}
</div>
<div id="container" class="foto_pelicula">
{% for p in peliculas %}
{% if p.genero.id == 2 %}
<img src={{p.urlPortada}} width="289" height="289"/></li>
{% endif %}
{% endfor %}
</div>
<div id="container" class="foto_pelicula">
{% for p in peliculas %}
{% if p.genero.id == 3 %}
<img src={{p.urlPortada}} width="289" height="289"/></li>
{% endif %}
{% endfor %}
</div>
{% endblock %}
The Images SRCs are urls that I take from the internet, I guess it does not really matter whether they are taken from the internet or stored in your proyect.
my CSS
#container{
width: 290px;
overflow: hidden;
margin: 5px 4px 0 auto;
padding: 0;
background: #222; /* FONDO DEL RECTANGULO CONTENEDOR */
border: 3px solid #8E1600;
float: left;
}
I added this new lines to my CSS but the image won't change.
.foto_pelicula > img:hover {
transform: scale(1.1);
}
Maybe I'm not using the proper lines in the CSS as I'm a junior programmer.
Help is much appreciated
This question has no connection to Django. In future try to create working html template for your purposes and then add it to django template.
You setup .foto_pelicula > img:hover to transform image. But > selector finds only direct child. See documentation on selectors.
And in your code direct child is a, but not img. Therefore you don't see result. You can change your css rule to .foto_pelicula > a > img:hover
See demo:
#container{
width: 290px;
overflow: hidden;
margin: 5px 4px 0 auto;
padding: 0;
background: #222; /* FONDO DEL RECTANGULO CONTENEDOR */
border: 3px solid #8E1600;
float: left;
}
.foto_pelicula > a > img:hover {
transform: scale(1.1);
}
<div id="container" class="foto_pelicula">
<img src="https://via.placeholder.com/289" width="289" height="289"/>
</div>
IMPORTANT
In your html code I see several divs with id="container". You can setup only one id with certain name to each page. That's why it called identifier.
So change your ids to classes or give different id names for this divs.

css - right link in navigation is lower

I have problem with right link in my bottom navigation.
This link is a little bit lower.
I am not css specialist, please for help.
My html code:
<div class="header container">
{% if is_paginated %}
{% if page_obj.has_previous %}
<h4>Previous</h4>
{% endif %}
<span class="header-label"><h4>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</h4></span>
{% if page_obj.has_next %}
<h4>Next</h4>
{% endif %}
{% endif %}
</div>
My css code:
.arrow {
position: absolute;
}
.right {
right: 0;
}
.left {
left: 0;
}
.container {
margin: 0 auto;
}
.header {
text-align: center;
}

How do I target unknown <div>s from the CSS

how do you add :target {opacity: 1} onto the href"#pop{{user.user_id}}" below?
the user_id is unknown until the page generates
The for loop as follows;
{% for user in pull %}
Link: {{user.user_name}} {{ user.user_id }}
<div style="opacity:0" id="pop{{user.user_id}}">
{{ user.info }}
</div>
{% endfor %}
Which looks like this after its translated onto a web page from Django.
Link: tom 10
<div style="opacity:0" id="pop10">
this is my info
</div>
Link: ann 8
<div style="opacity:0" id="pop8">
i am an apple
</div>
Link: mike 3
<div style="opacity:0" id="pop3">
i like pears
</div>
if the styles for all of them are same then you can add a class in the loop for each div and anchor and then add style to these anchors and divs which have this added class. If you want different styles for these divs and anchors then you need to have the id's .Hope it helps
{% for user in pull %}
<a class="styled_class" href="#pop{{user.user_id}}">Link: {{user.user_name}} {{ user.user_id }}</a>
<div class="styled_div" style="opacity:0" id="pop{{user.user_id}}">
{{ user.info }}
</div>
{% endfor %}
Okay, after a lot of thinking i figured it out by myself, all you need to do is make a style tag in the same template which is encased in the same for loop to generate the individual unique css Ids pointing to each div
<style type="text/css">
{% for i in pull %}
#pop{{i.user_id}}:target {
opacity: 1;
z-index: 1;
}
#pop{{i.user_id}} {
width: 58.8em;
min-height: 10em;
background: none repeat scroll 0% 0% #FFA500;
top: 9em;
border-radius: 16px;
opacity: 0;
position: absolute;
z-index: -1;
transition: opacity 1s ease 0s, z-index 1s cubic-bezier(0, 1, 1, 0) 0s;
max-height: 34em;
padding: 1em;
margin-left: 1em;
}
#pop_close{{i.user_id}}:target {
opacity: 0;
z-index: -1;
}
{% endfor %}
</style>