html - 高度 : 100% not working when parent div only has max-height

标签 html css google-chrome

无法绕过它!我尝试使用 max-height 使父 div 的高度不超过窗口的 80% , 和子 div 将所有父高度减去一些东西。

但是height以 parent 百分比表示的属性(property)对 child 根本不起作用,除非我给 parent height属性(property)。

这是为什么?

这是我的简化 fiddle :https://jsfiddle.net/mbatcer/m2ohnsf5/
为什么内部 div 不尊重父高度并超出容器?

HTML:

<div class="container">
  <div class="inner">
    <img src="http://via.placeholder.com/70x300">
  </div>
</div>

CSS:
.container {  
  background: blue; 
  padding: 10px; 
  max-height: 100px; 
}

.inner { 
  height: 100%;
  width: 70px;
  overflow: hidden;
}

最佳答案

我认为这就是你要找的:

.container {  
  background: blue; 
  padding: 10px; 
  max-height: 100px; 
  display: flex;

}

.inner { 
  width: 70px;
  overflow: hidden;
}

这是 fiddle :https://jsfiddle.net/f9sfczya/1/

在这里我添加了 display: flex;在您的父 div 中并移除高度:100%;
从子div。

不幸的是显示:flex; IE9 及更早版本不支持。

希望这可以帮助你。

关于html - 高度 : 100% not working when parent div only has max-height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45552111/

相关文章:

javascript - 滚动到 div 时 JS 计数器停止计数

html - Firefox 图像 div 未显示(适用于 chrome)

javascript - 如何访问扩展程序中的书签页面和其他 Chrome 网址?

google-chrome - Lighthouse 报告显示我的 start_url 未缓存

html - 在所有方向上偏移量为 0 的绝对位置

javascript - EaselJS 中的注册点是什么?

javascript - 使用 Javascript 添加内容时使 HTML div 展开

html5 getusermedia() 在没有 ssl 的情况下捕获视频

html - 页面高度的滚动条 div 大小

html - 单击外部时如何隐藏 div