我在试着理解对我来说似乎出乎意料的行为:

我在一个容器中有一个max-height为100%的元素,该容器也使用了max-height,但出乎意料的是,子元素溢出了父元素:

.container { 背景:蓝色; 填充:10 px; max-height: 200 px; max-width: 200 px; } img { 显示:块; max-height: 100%; max-width: 100%; } < div class = "容器" > <img src="http://placekitten.com/400/500" /> < / div >

但是,如果父节点被指定了显式的高度,这个值是固定的:

.container { 背景:蓝色; 填充:10 px; max-height: 200 px; max-width: 200 px; 身高:200 px; } img { 显示:块; max-height: 100%; max-width: 100%; } < div class = "容器" > <img src="http://placekitten.com/400/500" /> < / div >

有人知道为什么在第一个例子中子节点不尊重父节点的max-height吗?为什么需要显式高度?


当前回答

容器通常已经很好地包装了它们的内容。反之往往就不一样了:孩子们不能很好地满足他们的祖先。因此,在最里面的元素上设置宽度/高度值,而不是在最外面的元素上,并让外面的元素包装它们的内容。

.container {
    background: blue;
    padding: 10px;
}

img {
    display: block;
    max-height: 200px;
    max-width: 200px;
}

其他回答

http://jsfiddle.net/mpalpha/71Lhcb5q/

.container { 显示:flex; 背景:蓝色; 填充:10 px; max-height: 200 px; max-width: 200 px; } img { object-fit:包含; max-height: 100%; max-width: 100%; } < div class = "容器" > <img src="http://placekitten.com/400/500" /> < / div >

当你为子结点的max-height指定一个百分比时,它是父结点实际高度的百分比,而不是父结点max-height的百分比,这很奇怪。这同样适用于max-width。

当你没有在父节点上指定显式高度时,子节点的max-height就没有基础高度可供计算,因此max-height计算为none,允许子节点尽可能高。现在作用于子对象的唯一其他约束是父对象的max-width,由于图像本身的高度大于宽度,因此它向下溢出容器的高度,以保持其纵横比,同时仍然尽可能大。

当你为父节点指定显式高度时,子节点知道它最多只能是那个显式高度的100%。这允许它被限制到父元素的高度(同时仍然保持它的纵横比)。

与其使用max-height: 100%/100%,另一种填满所有空间的方法是使用position: absolute,将上/下/左/右设置为0。

换句话说,HTML看起来像下面这样:

<div class="flex-content">
  <div class="scrollable-content-wrapper">
    <div class="scrollable-content">
      1, 2, 3
    </div>
   </div>
</div>

.flex-content {
  flex-grow: 1;
  position: relative;
  width: 100%;
  height: 100%;
}

.scrollable-content-wrapper {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  overflow: auto;
}

.scrollable-content {
    /* Add styling here */
}

试试下面的方法:

.flex-content { flex-grow: 1; position: relative; width: 100%; height: 100%; } .scrollable-content-wrapper { position: absolute; left: 0; right: 0; top: 0; bottom: 0; overflow: auto; } html { height: 50%; width: 50%; } body { height: 100%; width: 100%; } .parent { height: 100%; outline: 1px solid red; } <html> <body> <div class="parent"> <div class="flex-content"> <div class="scrollable-content-wrapper"> <div class="scrollable-content" id="scrollable"> 1, 2, 3 </div> </div> </div> </div> <button onClick="scrollable.innerText += '\nSome more text'" style="margin-top: 1rem;">Add Line</button> <p> The red outline represents the parent. Click above to add a line until overflow occurs to see that the size of the parent is not increased. </p> </body> </html>

容器通常已经很好地包装了它们的内容。反之往往就不一样了:孩子们不能很好地满足他们的祖先。因此,在最里面的元素上设置宽度/高度值,而不是在最外面的元素上,并让外面的元素包装它们的内容。

.container {
    background: blue;
    padding: 10px;
}

img {
    display: block;
    max-height: 200px;
    max-width: 200px;
}

您可以使用属性object-fit

.cover {
    object-fit: cover;
    width: 150px;
    height: 100px;
}

就像这里建议的那样

Chris Mills在Dev.Opera中对这个属性的完整解释

还有一个更好的CSS-Tricks

它在

Chrome 31 +。 Safari 7 + 1。 Firefox 36 +。 歌剧26 +。 Android 4 . + 4。 iOS 8 +。

我刚刚检查了vivaldi和铬支持它以及(在这里没有惊喜)

目前IE上不支持,但是…谁在乎呢?此外,iOS支持对象匹配,但不支持对象定位,但很快就会支持了。