I need to make a square element that is based off of the width of the screen. To set the height the same as the width, I tried using JS, but it seems to get it slightly wrong. Here is an example
var square = document.getElementById('square');
square.style.height = square.clientWidth + 'px';
#square {
background-color: blue;
width: calc(20vw - 16px);
padding: 8px;
}
<div id="square">Element</div>
The blue "square" above is not square. Could any explain why? Instead of clientWidth
, I've tried scrollWidth
and offsetWidth
with similar results. I haven't gotten style.width
to give out a correct number either.
Even if you inspect, on Chrome, the blue square you get a number for the height and width that are close but still very different.