0

I want to know how to position multiple div's on each other, without position absolute in HTML.

I tried with position: absolute but due to this, I have to specify container div height explicitly, which I don't want to do.

danwellman
  • 9,068
  • 8
  • 60
  • 88
kunal
  • 123
  • 1
  • 10

2 Answers2

2

How do you want to place them exactly ?

If they are div, they should be on top of each other with position: static by defaults. If you don't want to use position: absolute, you could use negative margins. This is not a recommended solution, but the hack definitely works.

.d1 {
  background-color: red;
  height: 200px;
  width: 150px;
 }

.d2 {
  background-color: blue;
  height: 150px;
  width: 100px;
  margin-top: -100px;
 }
<div class="d1"></div>
<div class="d2"></div>

Note that you can use % margins if needed but the % margin properties will always be a percentage of the parent block WIDTH. So be careful with that.

NB : Tanks to @Oriol for correcting mistakes I made. I edited my answer thanks to his advice.

Carele
  • 756
  • 3
  • 13
  • Margin percentages are resolved according to the width of the containing block, which may be different than the width of the window. And the default is `position: static`, not `position: relative`. – Oriol Jul 01 '16 at 11:53
  • Thanks for pointing both things out. I made shortcuts in my explanation because the differences were not particularly relevent between what i mentionned and the reality. However this is inacurate and i appologize. I edit my post – Carele Jul 01 '16 at 13:52
0

Not sure what you're trying to achieve but I can imagine only one scenario, where something like that would be usefull. Namely switching between several divs. If that's the case use display:none on all but the current div.

In anyway child div is by default "overlapping" with parent div, so I assume what you mean is that you want siblings to be "on each other"... however that sounds.

The only way to do this is (except for negative margin hacks) absolute and relative positioning.

Here's how:

#foo{
  background-color:red;
  height: 50px;
  width: 50px;
  position: relative;
}
#bar{
  background-color:blue;
  height: 50px;
  width: 50px;
  position: absolute;
}
#foobar{
  background-color: green;
  height: 50px;
  width: 50px;
  position: absolute;
}
#raboof{
  background-color: yellow;
  height: 50px;
  width: 50px;
}

<div id="foo">
  <div id="bar"></div>
  <div id="foobar"></div>
  <div id="raboof"></div>
</div>

Jsfiddle: https://jsfiddle.net/t81hvsa1/

Keep in mind that: 1. You may but don't need to make last child absolutely positioned. 2. The last absolutely positioned child will always be on top.

Edit: I've just noticed, this question's discussion has all the answers you could possibly want; more elaborate and better formatted at that.

Community
  • 1
  • 1
P.Pal
  • 63
  • 9
  • Here, foo height is specified i want it to be auto. – kunal Jun 24 '16 at 04:06
  • It can well be auto height as long as you keep one of the child's position (preferrably the last one) anything but absolute or fixed. I've edited the [fiddle](https://jsfiddle.net/t81hvsa1/5/) and commented out the background colors of child divs, so you see that foo height extends to the the height of not absolutely positioned raboof. You change raboof height and foo's height adjusts to it automatically. – P.Pal Jul 01 '16 at 11:34