我在可調整大小的 div 中有一個影像,如下所示:

這是不正確的

而它應該是這樣的:

uj5u.com熱心網友回復:
您可以使用最大高度和寬度值,然后使用 object-fit 屬性強制影像的比例
#img_wrapper {
resize: both;
overflow: hidden;
background-color: gray;
height: 15rem;
width: 15rem;
min-height: 15rem;
min-width: 15rem;
user-select: none;
}
#img_testing {
display: block;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: auto;
width: 70%;
max-width: 70%;
max-height: 70%;
object-fit: contain;
}
<div id="img_wrapper">
<img src="https://i.ibb.co/zJvjmKs/icon.png" id="img_testing">
</div>
uj5u.com熱心網友回復:
試試這個!
img{
max-width: 100%;
max-height: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 10px;
box-sizing: border-box;
}
div{
position: relative;
background-color: gray;
}
div:nth-of-type(1){
width: 150px;
height: 150px;
}
div:nth-of-type(2){
width: 300px;
height: 150px;
}
div:nth-of-type(3){
width: 150px;
height: 300px;
}
<div>
<img src="https://i.ibb.co/zJvjmKs/icon.png">
</div>
<br>
<div>
<img src="https://i.ibb.co/zJvjmKs/icon.png">
</div>
<br>
<div>
<img src="https://i.ibb.co/zJvjmKs/icon.png">
</div>
謝謝和最好的問候!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/478187.html
上一篇:隨著Safari中輸入文本的變化,輸入型別文本下方的div上下移動
下一篇:詳細資訊時隱藏另一個元素[打開]
