div盒子宽高固定,图片正常显示

2020-08-28  本文已影响0人  努力study代码的小哪吒

div盒子大小固定,图片要求正常铺满显示

<div class="img1">
   <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598615359091&di=2bef5c23d4e801ffdb7a349243142ff0&imgtype=0&src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190825%2F15%2F1566717149-GjgslpqWvb.jpg" />
</div>
.img1{
    width 400px
    height 300px
    display flex
    align-items center
    justify-content center
    img{
      width 100%
      height 100%
    }
  }

图片显示如下:


图一(变形图片)
.img1{
    width 400px
    height 300px
    display flex
    align-items center
    justify-content center
    overflow hidden
    img{
      max-width 100%
    }
  }

图片显示如下:


图二(图片被截断)
.img1{
    width 400px
    height 300px
    display flex
    align-items center
    justify-content center
    overflow hidden
    img{
      width 100%  
      height 100%
      object-fit contain
    }
  }

图片显示如下:


图三(会有留白)
.img1{
    width 400px
    height 300px
    display flex
    align-items center
    justify-content center
    overflow hidden
    img{
      width 100%  
      height 100%
      object-fit cover
    }
  }

图片显示如下:


图三(会有留白)
上一篇下一篇

猜你喜欢

热点阅读