簡體   English   中英

CSS 如何在圖像寬度更大時填充 div 寬度,當圖像高度更大時如何填充高度

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

有沒有一種方法可以在包含圖像的高度大於其寬度時填充 div 的高度,反之亦然,只有 CSS 而不擴展圖像的框。

我做了一個代碼筆來更好地解釋它

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

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

您必須分別為 img 提供高度和寬度,並為包含 img 的 div 設置 display flex

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM