absolute
.box div {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
border: 1px solid black;
display: block;
background-color: rgba(0,0,0,0.1);
}
不确定这是否比您提出的更好,也许您想要在框中添加内容?
.parent {
width: 50vmin;
height: 50vmin;
background-color: blue;
margin: 0 auto;
}
.child {
width: 100%;
height: 100%;
background-color: red;
}
HTML:
<div class='parent'>
<div class='child'></div>
</div>
absolute
定位。
css-padding-trick-responsive-intrinsic-ratios
因此,使用absolute
定位内部div。.box {
...
position: relative;
}
.box div {
...
position: absolute;
left: 0;
top: 0;
}
right: 0; bottom: 0;
是不必要的。height:100%
就可以工作。 - Felix A J在子元素 div
中添加 padding-bottom: 100%
可以填充空间,似乎是一种解决方法;更新的 jsfiddle 反映了这一点。
height: 38vw; width: 38vw;
- Christian Lundahl