将img限制在div并保持宽高比?

将img限制在div并保持宽高比?

问题描述:

我试过了我能找到的所有东西。将img限制在div并保持宽高比?

我试图保持一个图像溢出它在里面的div。

我已经试过:对这些值的

div.row #whoPic { 
    object-fit: contain; 
    max-width: 100%; 
    max-height: 100%; 
    overflow: hidden; 
    max-width: auto; 
    max-height: auto; 
    max-width: 300px; 
    max-height: 300px; 
} 

...和许多变化。

我也试过针对所有的img,以及非常精确的目标。

请帮我一把。在这里坚果。

+0

您使用CSS的是整个块? –

+1

可能的重复[如何自动调整图像以适合div容器](https://stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit- a-div-container) –

事实上,这个问题之前已经被问过了。
这里:How do I auto-resize an image to fit a div container
这里:Make an image width 100% of parent div, but not bigger than its own width
这里:Contain an image within a div?
这里:How do I fit an image (img) inside a div and keep the aspect ratio?

max-widthmax-height和均设定为100%限制图像。

.theContainer { 
 
    width: 300px; 
 
    height: 100px; 
 
    background-color: lightblue; 
 
} 
 

 
.theContainer img { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
}
<div class="theContainer"> 
 
    <img src="https://placehold.it/200x200"> 
 
</div> 
 
<h3> The original image size is 200x200 </h3> 
 
<img src="https://placehold.it/200x200">

+1

''不使用或需要结束斜线。 – Rob

+0

@Rob我从来没有真正确定,但没问题,谢谢! –

+0

这是否也适用于其他对象,如iframe和表单?我有一种表格也会离开它的div,但不管我做了什么,我都不能让它留在那里,或者当窗户变小时让div改变它的高度。 – David