You do not need JavaScript
to maintain the ratio of Width
to Height
, if you want to keep Height
proportional to Width
, set Height
with vw (viewport width)
.
If you need Width to be proportional to Height, set Width
to vh (viewport height)
.
As you want a perfect square, that is, a ratio of 1: 1, then the vw
of height
will be equal to %
of width
.
Here is an example below:
.bloco {
float: left;
}
.bloco20 {
background-color: teal;
width: 19%;
height: 19vw;
margin: 0.5%;
}
.bloco40 {
background-color: crimson;
width: 39%;
height: 39vw;
margin: 0.5%;
}
<div class="bloco bloco20"></div>
<div class="bloco bloco20"></div>
<div class="bloco bloco20"></div>
<div class="bloco bloco20"></div>
<div class="bloco bloco20"></div>
<div class="bloco bloco40"></div>
<div class="bloco bloco40"></div>
<div class="bloco bloco20"></div>
<div class="bloco bloco20"></div>