CSS溢出: relative height with bottom absolute positionning

标签 css overflow positioning

是的,另一个关于 css 的烦人的溢出问题......

这是我的案例:

HTML

<div id="parent">
  <div id="child1">
    Some short content that may take a line or two.
  </div>
  <div id="child2">
    Some short to very long content that may overflow parent div...
  </div>
<div>

CSS

#parent {
  position: absolute;
  height: 100%;
  width: 100px;
}

#child1 {
}
#child2 {
  overflow: auto;
}

如您所见,我希望 child2 在需要时溢出父 div。但由于我不知道 child2 的确切高度(因为 child1 可能会有所不同),我无法像习惯 bottom: 0px 那样进行一些绝对定位和top: ???px .

一些可以玩的 JSFiddle :https://jsfiddle.net/6r3ojecL/1/

在最坏的情况下,我会使用一些丑陋的 JS 代码片段,但如果我能再次掌握 css,我会很高兴。 :)

谢谢!

最佳答案

使用display: flex的解决方案。检查updated fiddle

#parent {
  display: flex;
  position: absolute;
  height: 50%;
  width: 100px;
  border: 1px solid red;
  background-color: red;
  flex-direction: column;
}
#child1 {
  height: 50px;
  background-color: yellow;
}
#child2 {
  flex: 1;
  background-color: green;
  overflow: auto;
}
<div id="parent">
  <div id="child1"></div>
  <div id="child2">
    child 2 content child 2 content child 2 content child 2 content child 2 content child 2 content child 2 content child 2 content child 2 content
  </div>
</div>

关于CSS溢出: relative height with bottom absolute positionning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140916/

相关文章:

css - 可滚动的 div 填充可用高度

css - 响应式页面 - 溢出问题

html - 如何为溢出隐藏的div提供填充底部?

html - 让 Div 填充页眉和页脚之间的区域

html - 如何调整 space-around 的间距?

html - ionic ion-nav-title自定义内容错误显示

html - 在 h2 上添加 margin

css - 定位框架集或框架

html - float 输入留在 div 中

html - 如何使整个 float 元素 block 与其父容器的中心对齐?