tags:

views:

600

answers:

4

its been a while since ive had to do major css work, so im stuck

im trying to align a child div tag to the bottom or baseline of the parent div tag.

HTML

<div id="parentDiv">
<div class="childDiv"></div>
</div>

CSS

#parentDiv
{
  width:300px;
  height:300px;
  background-color:#ccc;
  background-repeat:repeat
}
#parentDiv .childDiv
{
  height:100px;
  width:30px;
  background-color:#999;
}

All i want to do is have the child Div at the basline of the Parent Div, here is what it looks like right now...

Note i will have multiple childDiv's and they may all vary in height, and ill need them all to align to the baseline/bottom.

Thanks for all help

A: 

You would probably would have to set the child div to have position: absolute.

Update your child style to

#parentDiv .childDiv
{
  height:100px;
  width:30px;
  background-color:#999;
  position:absolute;
  top:207px;
}
Ravia
i tried that, but what i need is the bottom of the childDiv to sit at the bottom of the parentDiv, the graph is to be dynamic, and the heights will vary from each childDiv to the next...positioning might work but i havent quit figured out something that is solid...
sia
I have tried this and is working for me
Ravia
not a good idea to set top to 207 (some arbitrary number) better to set bottom:0
pstanton
+4  A: 

You need to add this:

#parentDiv
{
  position:relative;
}
#parentDiv .childDiv
{
  position:absolute;
  bottom:0;
}

When declaring absolute element, he positioned according to his nearest-parent that is non-static (means: that is absolute, relative or fixed).

Y. Shoham
Good point from *pstanton* below: you should add `left:0;` (or `right:0;`) as well; some browsers (Chrome+Safari?) may not understand this alone.
Y. Shoham
+1  A: 

this works (i only tested ie & ff):

<html>
<head>
    <style type="text/css">
        #parent {height:300px; width:300px; background-color:#ccc; border:1px solid red;
            position:relative;}
        #child  {height:100px; width:30px; background-color:#eee; border:1px solid green;
            position:absolute; bottom:0; left:0;}
    </style>
</head>
<body>
    <div id="parent">parent
        <div id="child">child</div>
    </div>
    outside
</body>
</html>

hope that helps.

pstanton
A: 

I noticed that you must specify the height of the child for this solution to work.

What if you have 2 or more children that you want to align to the bottom of the parent and the height of the children is predetermined by its contents? What solution would work for this type of situation?

tayls