【发布时间】:2019-07-08 22:53:36
【问题描述】:
我需要制作一个基于屏幕宽度的方形元素。要将高度设置为与宽度相同,我尝试使用 JS,但似乎有点错误。这是一个例子
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>
上面的蓝色“正方形”不是正方形。有谁能解释为什么?我尝试了scrollWidth 和offsetWidth 而不是clientWidth,结果相似。我也没有让style.width 给出正确的号码。
即使您在 Chrome 上检查蓝色方块,您也会得到一个高度和宽度的数字,它们很接近但仍然非常不同。
【问题讨论】:
标签: javascript html css css-calc