在图片长宽不相等的情况下,想将长宽设置为相等并且自适应屏幕,可以通过 js 的方式进行设置并通过监听 resize 来实时更新,但是这种方式很麻烦。
<div class='box'>
<img src="https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1504583607&di=3f5b4637a7b70d018ad9588c78971956&src=http://pic.baike.soso.com/p/20140327/20140327161206-845392828.jpg">
</div>
.box{
position: relative;
width: 50%;
height: 0;
padding-bottom: 50%;
}
.box img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
padding-bottom的值为百分比时: 定义基于父元素宽度的百分比下内边距。此值不会如预期地那样工作于所有的浏览器中。