[英]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.