Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align an absolute DIV of unknown height outside a "relative" parent by CSS?

Consider a simple example with html

<div class="parent">
    <div class="child">
    </div>
</div> 

and CSS

.parent{
    position:relative;
    background:red;
    width:200px;
    height:40px;
}
.child{
    position:absolute;
    top:40px;
    left:30px;
    width:70px;
    height:70px;
    background:blue;
}

to place a DIV with absolute position just beneath its parent (with relative position). enter image description here

In this example, I equaled the absolute's top to the parent relative's height.

How to align the absolute DIV just under the parent when the height is unknown (both parent and child)?

like image 466
Googlebot Avatar asked Feb 08 '13 13:02

Googlebot


3 Answers

Didn't think this would work myself, but it seems to:

html, body {
    height: 100%;
}
.parent{
    position:relative;
    background:red;
    width:200px;
    height:40px;
}
.child{
    position:absolute;
    top:100%;
    left:30px;
    width:70px;
    height:70px;
    background:blue;
}
like image 169
Simon Avatar answered Nov 10 '22 11:11

Simon


Check this..

HTML:
-------

<div class="parent">
</div> 
<div class="child">
</div>

CSS:
-----
.parent{
    position:relative;
    background:red;
    width:200px;
    height:40px;
}
.child{
    position:absolute;
    top:auto;
    left:30px;
    width:70px;
    height:70px;
    background:blue;
}

(Example)

like image 24
Sri Avatar answered Nov 10 '22 09:11

Sri


you can use negative value for bottom, eg. bottom: -100px

EDIT: here is better solution: http://jsfiddle.net/mqy4z/3/ - set child's position to top:100%

like image 1
pwolaq Avatar answered Nov 10 '22 11:11

pwolaq