是的,另一个关于 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/