To maintain a div
with a background-image
responsive and in proportion, you should do the following.
Let’s say you have an image the size of 2000 x 1250, and you want to keep that ratio in any screen size or any space:
The formula you should use is (altura / largura) * 100
, then we have: (1250 / 2000) * 100 = 62.5
.
This value shall be defined as a percentage of padding-bottom
of the div that will have the background image, but you should not spend any time, leave as 0
.
Then just define the background-size
as cover
to keep the image the same size as the div
.
See the example below working on https://jsfiddle.net/diancabral/mhfb24q2/1/
HTML:
<div class="img__box">
<div class="img__src"></div>
</div>
CSS:
.img__box {
float: left;
width: 50%;
}
.img__src {
float: left;
/* Deve pegar a largura do elemento pai */
width: 100%;
/* O height é zero mesmo, a altura vai ser definida no padding-bottom */
height: 0;
/* Aqui você deve colocar o resultado da formula como porcentagem */
padding-bottom: 62.5%;
background: url(http://folhanobre.xyz/2015/09/Paisagem_imagem_linda_-52.jpg);
background-size: cover;
}
If you want to do this with standing images, just invert the formula and calculate (largura / altura) * 100
.
A Fiddle and the HTML used could help to respond better
– Vinícius