Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying one div on top of another

Tags:

html

css

I have some HTML with two divs:

<div>   <div id="backdrop"><img alt="" src='/backdrop.png' /></div>   <div id="curtain" style="background-image:url(/curtain.png);background-position:100px 200px; height:250px; width:500px;">&nbsp;</div> </div> 

I want the second div #curtain to appear on top of the div #backdrop. The two divs are the same size, however I'm not sure how to position the second div on top of the other.

like image 514
Villager Avatar asked Mar 28 '11 00:03

Villager


People also ask

How do I show a DIV on top of another?

Use CSS position: absolute; followed by top: 0px; left 0px; in the style attribute of each DIV. Replace the pixel values with whatever you want. You can use z-index: x; to set the vertical "order" (which one is "on top"). Replace x with a number, higher numbers are on top of lower numbers.

How do I make two divs stack on top of each other?

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

How do I create a DIV top layer?

In order to pull an html element out of the natural flow of how the elements are layed out on the screen you need to use position: absolute. This will allow the element to become a layer above the other elements (assuming that the z-index value is greater than all other's).


2 Answers

Use CSS position: absolute; followed by top: 0px; left 0px; in the style attribute of each DIV. Replace the pixel values with whatever you want.

You can use z-index: x; to set the vertical "order" (which one is "on top"). Replace x with a number, higher numbers are on top of lower numbers.

Here is how your new code would look:

<div>   <div id="backdrop" style="z-index: 1; position: absolute; top: 0; left: 0;"><img alt="" src='/backdrop.png' /></div>   <div id="curtain" style="z-index: 2; position: absolute; top: 0; left: 0; background-image:url(/curtain.png);background-position:100px 200px; height:250px; width:500px;">&nbsp;</div> </div> 
like image 189
Unsigned Avatar answered Oct 05 '22 13:10

Unsigned


There are many ways to do it, but this is pretty simple and avoids issues with disrupting inline content positioning. You might need to adjust for margins/padding, too.

#backdrop, #curtain {   height: 100px;   width: 200px; }  #curtain {   position: relative;   top: -100px; } 
like image 21
coreyward Avatar answered Oct 05 '22 13:10

coreyward