views:

2315

answers:

4

Hi,

Is it possible to stack up multiple DIVs like:

<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>

So that all those inner DIVs have the same X and Y position? By default they all go below each other increasing the Y position by the height of the last previous DIV.

I have a feeling some sort of float or display or other trick could bite?

EDIT: The parent DIV has position relative, so, using position absolute does not seem to work.

+1  A: 

style="position:absolute"

Dave Swersky
+4  A: 

Position the outer div however you want, then position the inner divs using absolute. They'll all stack up.

<style type="text/css">
.inner {
  position: absolute;
}
</style>

<div class="outer">
   <div class="inner">1</div>
   <div class="inner">2</div>
   <div class="inner">3</div>
   <div class="inner">4</div>
</div>
Matt
It doesn't seem to work. Maybe I should have mentioned that I have this scenario:<div style="position: absolute..."><div style="position: relative..."><div>stack this</div><div>stack this</div><div>stack this</div><div>stack this</div></div></div>
rFactor
You want to absolutely position the divs that have "stack this" in them. It does work - I tried it before posting my original. If you don't put class selectors on your divs, adapt the div selection method in Eric's answer to select the stack divs.
Matt
+3  A: 

To add to Dave's answer:

div { position: relative; }
div div { position: absolute; top: 0; left: 0; }
Eric Wendelin
It doesn't seem to work. Maybe I should have mentioned that I have this scenario: <div style="position: absolute..."> <div style="position: relative..."> <div>stack this</div> <div>stack this</div> <div>stack this</div> <div>stack this</div> </div> </div>
rFactor
I *think* in that case you want to set "top: 0; left: 0;" on your div that has "position: relative". Definitely test IE6 on that, though as I can't say for certain that it'll work.
Eric Wendelin
+1  A: 

If you mean by literally putting one on the top of the other, one on the top (Same X, Y positions, but different Z position), try using the z-index CSS attribute. This should work (untested)

<div>
    <div style='z-index: 1'>1</div>
    <div style='z-index: 2'>2</div>
    <div style='z-index: 3'>3</div>
    <div style='z-index: 4'>4</div>
</div>

This should show 4 on the top of 3, 3 on the top of 2, and so on. The higher the z-index is, the higher the element is positioned on the z-axis. I hope this helped you :)

Jimmie Lin