April 2, 2012

How to position divs on top of one another

Question by Bobby Francis Joseph

I want to position divs on top of one another. I have created a fiddle. I have three divs. Each div contains an image. I want to position divs so that the div having the largest image is at the bottom and the one having smallest image is on top.

http://jsfiddle.net/bobbyfrancisjoseph/7spcZ/2/

http://jsfiddle.net/bobbyfrancisjoseph/7spcZ/2/show/

Answer by Muhammad Sannan

Use position absolute with z-index.

use below css

#page-left {
float: left;
margin:5px;
}

#page-right {
float: left;
margin:5px;
}

#largest {
position: absolute;
    z-index: 1;
}
#medium {
position: absolute;
    z-index: 2;
}
#smallest {
position: absolute;
    z-index: 3;
}

http://jsfiddle.net/sannankhalid/sc8qE/

Answer by Starx

You can use z-index & position: absolute to treat the elements as layers. But as per your requirement and element order, a simple position: absolute is enough

#page1 div {
    position:absolute;
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!