tags:

views:

34

answers:

1

I have some HTML codes like

<div id="top" style="height: 50px"></div>
<div id="bottom" style="height: 50px"></div>
<div id="content">Here goes some text</div>

I want the middle div (#bottom) to be positioned on top of the div (#top) and while doing so, the #content div should automatically move up by 50px.

If i code like

<div id="bottom" style="height: 50px; position: relative; top: -50px;"></div>

the #bottom div do moves up but the #content div stays behind.. leaving a 50px gap between.

So how should I position it??

+2  A: 

If I'm understanding correctly, you want to take #bottom and remove it from the regular page flow, placing it over-top-of #top.

Two ways to take an element out of the regular page flow are position:float; and position:absolute;

Not knowing what the rest of your page looks like I suggest something like:

<div id='container' style='position:relative'>
  <div id="top" style="height: 50px"></div>
  <div id="bottom" style="height: 50px; position:absolute; top:0em; left:0em;"></div>
  <div id="content">Here goes some text</div>
</div>

That will put #bottom in the top, left-hand corner of #container, which is also where #top will be. #container being part of the regular page flow will be right below #top.

For centering an absolutely positioned element you can do like this:

<div style="display:table; margin: 0 auto;">  <!-- display:table; to 'shrink-wrap' the div - margin: 0 auto; to center it->
    <div style="position: relative; float:left;"> <!-- float also shrink-wraps -->
        <div id='top'>top div content</div>
        <div id='bottom' style="position:absolute; top:0em; width:100%; text-align:center;"> content of bottom div </div>
        <div id='content'></div>
    </div>
</div>
SooDesuNe
Thanks, and yes.. that is exactly what I mean. But now i want that #bottom in the middle. I was using m"argin: 0 auto;", now it's not working. so how do i put it in the middle of the page.. or #container??
ptamzz
you want to 'center an absolutely positioned element of unknown size'. See my updated answer
SooDesuNe
Thanks. That helped me!!
ptamzz