简体   繁体   English

HTML适合父母的父母的形象

[英]HTML fit image to parent's parent

I want to fit an image inside some divs and make it as big as possible without changing the aspect ratio. 我想在某些div内放入一个图像,并使其尽可能大而不更改纵横比。 Setting max-height and max-width to 100% on both the image and its parent does not work and the image overflows (I guess because the parent does not really have a width or height so the image can not resize to it?). 将图像及其父对象的max-height和max-width都设置为100%无效,并且图像溢出(我猜是因为父对象实际上没有宽度或高度,所以图像无法调整为它的大小?)。

JSFiddle JSFiddle

<div class="block">
  <div class="row">
    <div class="col-xs-9">
      <div class="thumbnail">
        <img class="placeholder" src="https://unsplash.it/900/600" alt="">
      </div>
    </div>
    <div class="col-xs-3">
    </div>
  </div>
</div>

Edit: 编辑:
I will clarify what I really want to achieve later this evening when I have time to write. 我将在今晚晚些时候有时间写时澄清我真正想要实现的目标。

Set the the image as background image to .thumbnail , and constrict it's size using background-size ( demo ): 将图像设置为.thumbnail作为背景图像,并使用background-sizedemo )限制其大小:

.thumbnail {
  display: inline-block;
  margin: 0;
  height: 100%;
  width: 100%;
  background: url("https://unsplash.it/900/600") no-repeat;
  background-size: contain;
}

If you want the parent div to match its content, add display:table; 如果希望父div与其内容匹配,请添加display:table; in your "outline-block" css class. 在您的“概述块” css类中。

Fiddle 小提琴

Similar post on stack overflow: how-do-i-auto-resize-an-image-to-fit-a-div-container 堆栈溢出的类似文章: 如何自动调整图像大小以适合div容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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