Stacking DIVs on top of each other?
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.
Solution 1:
Position the outer div however you want, then position the inner divs using absolute. They'll all stack up.
.inner {
position: absolute;
}
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
<div class="inner">4</div>
</div>
Solution 2:
To add to Dave's answer:
div { position: relative; }
div div { position: absolute; top: 0; left: 0; }
Solution 3:
You can now use CSS Grid to fix this.
<div class="outer">
<div class="top"> </div>
<div class="below"> </div>
</div>
And the css for this:
.outer {
display: grid;
grid-template: 1fr / 1fr;
place-items: center;
}
.outer > * {
grid-column: 1 / 1;
grid-row: 1 / 1;
}
.outer .below {
z-index: 2;
}
.outer .top {
z-index: 1;
}