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

后端 未结 12 2048
广开言路
广开言路 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:27

    I was having a lot of problems to get this working, every single solution I found didn't seem to work.

    I realized that I had to set the div display to flex, so basically this is my CSS:

    div{
    display: flex;
    }
    
    div img{ 
    max-height: 100%;
    max-width: 100%;
    }
    

提交回复
热议问题