Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the wrapping div element with relative position match child elements height?

Tags:

html

css

I have a simple problem where I have 2 divs, 1 is relative positioned. The child element is absolute positioned. This child has varying height.

The code so far:

HTML

<div id="wrap"><div id="inner"></div></div>

CSS

#wrap {
    width: 100%;
    background: #ccc;
    position: relative;
    min-height: 20px;
}

#inner {
    width:30%;
    background: #000;
    position: absolute;
    right: 0;
    top: 0;
    height: 200px;
}

The problem I have is that the #wrap element doesn't adjust its height to match the child element and therefor the child element hangs outside the edge of the parent. Can this be done with relative and absolute positioned elements?

I know this can be achieved with floating elements and following them with css => cleared: both, but I'd like to know if its possible with this method.

I've created a jsfiddle of this problem over here if anybody would like to help me out!

Thanks a lot.

like image 590
willdanceforfun Avatar asked Jun 12 '11 01:06

willdanceforfun


People also ask

How do I position a div relative to another?

First set position of the parent DIV to relative (specifying the offset, i.e. left , top etc. is not necessary) and then apply position: absolute to the child DIV with the offset you want. It's simple and should do the trick well.

How do you position an element relative to a viewport?

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.

What is position relative CSS?

position: relative; changes the position of the element relative to the parent element and relative to itself and where it would usually be in the regular document flow of the page. This means that it's relative to its original position within the parent element.


2 Answers

Absolute positionned elements are outside the flow so parents can't adjust their height.

But you can simply use:

#wrap {
    width: 100%; /* useless */
    background: #ccc;
    overflow:hidden; /* establish a new formatting context */
    min-height: 20px;
}

#inner {
    width:30%;
    background: #000;
    float:right;
}
like image 75
MatTheCat Avatar answered Sep 16 '22 13:09

MatTheCat


No, you can't make a parent with position: relative adjust its height to fit a child element with position: absolute.

This is because:

In the absolute positioning model, a box is explicitly offset with respect to its containing block. It is removed from the normal flow entirely (it has no impact on later siblings). An absolutely positioned box establishes a new containing block for normal flow children and absolutely (but not fixed) positioned descendants. However, the contents of an absolutely positioned element do not flow around any other boxes.

http://www.w3.org/TR/CSS21/visuren.html#absolute-positioning

If you wanted to stick with your position based code, you'd have to use JavaScript to set the height of the parent div.

Otherwise, stick to using floats if they work for your case. @MatTheCat's answer looks good to me.

Just for completeness, here's a demo of @MatTheCat's answer with height: 200px added, so you can see the parent div does adjust in height: http://jsfiddle.net/gR2wL/3/

like image 27
thirtydot Avatar answered Sep 18 '22 13:09

thirtydot