CSS force image resize and keep aspect ratio

前端 未结 23 1267
野趣味
野趣味 2020-11-22 10:04

I am working with images, and I ran across a problem with aspect ratios.

\"\"

23条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 10:59

    Very similar to some answers here, but in my case I had images that sometimes were taller, sometimes larger.

    This style worked like a charm to make sure that all images use all available space, keep the ratio and not cuts:

    .img {
       object-fit: contain;
       max-width: 100%;
       max-height: 100%;
       width: auto;
       height: auto;
    }
    

提交回复
热议问题