I have a problem with a div not being clipped to the parent even though it has overflow: hidden.
I've looked through the overflow: hidden questions here on stackoverflow but most of them either have problems with position or seem to suggest that my code should work.
Here's a MWE, you can find the jsfiddle here:
<div id="parent">
<div id="scroller">
<div id="child">
meh
</div>
</div>
</div>
CSS:
#parent {
height: 500px;
overflow: hidden;
}
#scroller {
overflow: scroll;
}
#child {
height: 10000px;
}
What I expect
#parent has overflow: hidden so #scroller gets clipped to the height of parent. Because its #child is taller than the resulting height overflow: scroll results in a scrollbar.
What happens
#scroller just uses the height of #child and ignores both overflow properties.
What about simple workarounds?
- In my real world problem there is multiple
<div>s in#parentso I can't give#scrollera height. - The html is generated automatically so I can't just remove
#scroller.
Thanks for all help, Stefan
ANSWER
There actually is a CSS-only answer in the comments with display: flex. See:
https://jsfiddle.net/huocukw7/6/
#parent {
height: 500px;
overflow: hidden;
display: flex;
flex-direction:column;
}
#scroller {
overflow: auto;
flex-grow:1;
}
#child {
height: 10000px;
}
display:flex: jsfiddle.net/huocukw7/6