简体   繁体   中英

How to fill up div width when image width is more and height when image height is more with CSS

Is there a way to fill up a div's height when its containing image's height is more than its width and vice versa with only CSS without extending the image's box.

I've made a codepen to explain it better

https://codepen.io/boidu/pen/xxPYvQV

.img-container{
    height: 15rem;
    width: 15rem;
    img{
        height: 100%;
        width:100%;
        object-fit: contain;
    }
}

You have to give height and width separately to the img and set display flex as below for the div containing img

display: flex;
flex-direction: row;
align-items: center;
justify-content: center;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM