Scale image to fit a bounding box

前端 未结 13 2054
轻奢々
轻奢々 2020-11-27 10:17

Is there a css-only solution to scale an image into a bounding box (keeping aspect-ratio)? This works if the image is bigger than the container:

img {
  max-         


        
13条回答
  •  情歌与酒
    2020-11-27 10:52

    html:

        

    css:

    .container{
      width: 100px;
      height: 100px;
    }
    
    
    .flowerImg{
      width: 100px;
      height: 100px;
      object-fit: cover;
      /*object-fit: contain;
      object-fit: scale-down;
      object-position: -10% 0;
      object-fit: none;
      object-fit: fill;*/
    }
    

提交回复
热议问题