<div class="constant-width-to-height-ratio"></div>
.constant-width-to-height-ratio {
background: #333;
width: 20%;
}
.constant-width-to-height-ratio::before {
content: '';
padding-top: 100%;
float: left;
}
.constant-width-to-height-ratio::after {
content: '';
display: block;
clear: both;
}
在::before伪元素上,等宽高比添加top会导致元素的高度等于其宽度的百分比。因此,100%意味着元素的高度将始终是宽度的100%,从而创建一个响应的正方形。