How do I fit an image (img) inside a div and keep the aspect ratio?

后端 未结 12 2027
广开言路
广开言路 2020-11-28 01:31

I have a 48x48 div and inside it there is an img element, I want to fit it into the div without losing any part, in the mean time the ratio is kept, is it achievable using h

12条回答
  •  余生分开走
    2020-11-28 02:20

    Unfortunately max-width + max-height do not fully cover my task... So I have found another solution:

    To save the Image ratio while scaling you also can use object-fit CSS3 propperty.

    Useful article: Control image aspect ratios with CSS3

    img {
        width: 100%; /* or any custom size */
        height: 100%; 
        object-fit: contain;
    }
    

    Bad news: IE not supported (Can I Use)

提交回复
热议问题