用 CSS 实现固定宽高比
用 CSS 实现固定宽高比
使用 padding 实现宽高比
<div class="container">
<div class="img-wrap">
<img src="//cdn.wallleap.cn/img/pic/illustrtion/202207261246820.png" alt="cover">
</div>
</div>
<style>
*, *::before, *::after{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
.container {
width: 80vw;
background: #EEE;
height: 400px;
}
.img-wrap {
width: 100%;
height: 0;
background: #77CCCC50;
padding-bottom: calc(10 / 16 * 100%);
overflow: hidden;
}
.img-wrap img {
width: 100%;
}
</style>CSS3 中 aspect-ratio 属性

最后更新于