I have a container div with a child div set to position absolute relative to its parent. The container div has its min – height set however when the child div’s height expands over the height of the parent the parent doest stretch. This is due to the absolute positioning of the child. Is there any ideas how i can get the parent to stretch with the child as its height increases
#parent{
position:relative;
min-height:200px;
width:200px;
}
#child{
position:absolute;
top:0;
left:0;
min-height:150px;
}
<div id="parent">
<div id="child"></div>
</div>
What you are trying to achieve is plain impossible,just for the simple fact that when you add an absolute rule to an element, you are implicitly taking it out of its normal layout context. Being in a relative parent container only means that it has a defined box that will contain it and set the default x and y coordinates of that element, by default it’s the window and that’s why when the position relative is not assigned to the parent absolute positioned elements will be relative to the top-left of the browser window.
Now an alternative could be to use an overflow hidden in the parent and have a scroll bar for the remaining content. See my example on jsfiddle: