使用this guide来保持宽高比。我一直使用它来拍摄照片和视频(使youtube视频非常容易调整大小)。
注意:这只会在您事先知道图像的宽高比,或者添加一些逻辑来确定它时才起作用。如果你想要处理任何未知尺寸的图像,这是行不通的。
类似HTML
<div class='imageResizeAccountInfo'>
<div class='content'>Aspect ratio of 1:1</div>
</div>
添加这个CSS
.imageResizeAccountInfo{
position: relative;
width: 100%;
}
.imageResizeAccountInfo:before{
content: "";
display: block;
padding-top: 100%; /* initial ratio of 1:1*/
}
.content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
而对于定制
/* Other ratios */
.ratio2_1:before{
padding-top: 50%;
}
.ratio1_2:before{
padding-top: 200%;
}
.ratio4_3:before{
padding-top: 75%;
}
.ratio16_9:before{
padding-top: 56.25%;
}
使用vor max-width和height设置为auto的绝对值对我来说似乎很好。顺便说一句:检查出_object-fit_属性,它对图像做了什么,我们已经可以用背景了。那里还有一个polyfill。 – kleinfreund