Scale image to fit a bounding box

前端 未结 13 2055
轻奢々
轻奢々 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:50

    Are you looking to scale upwards but not downwards?

    div {
        border: solid 1px green;
        width: 60px;
        height: 70px;
    }
    
    div img {
        width: 100%;
        height: 100%;
        min-height: 500px;
        min-width: 500px;
        outline: solid 1px red;
    }
    

    This however, does not lock aspect-ratio.

提交回复
热议问题