Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position a div element above another one

Tags:

html

css

In the picture below, I am wanting to place the driftwood/bomb image over the image directly above it; hence, I want to remove/collapse the "space" between these two divs. The gap, however, is not caused by the markup itself, because as you can see the "bomb" is making the picture bigger on the height.

enter image description here

I would like to position the navigation bar on the "header" (so the brown top of the navigation is just below the header bottom), so the gap disappears. These images are meant to overlap.

I assume this can be done using CSS. But how? Whatever solution needs to work cross-browser.

HTML:

<header></header>
<nav></nav>

CSS:

header {
    width: 980px;
    height: 327px;
    background: url(../images/header.png);
}

nav {
    width: 980px;
    height: 180px;
    background: url(../images/menu.png);
}
like image 337
John Svensson Avatar asked Feb 26 '12 12:02

John Svensson


People also ask

How do you make a DIV position relative to another DIV?

First set position of the parent DIV to relative (specifying the offset, i.e. left , top etc. is not necessary) and then apply position: absolute to the child DIV with the offset you want. It's simple and should do the trick well.

How do you stack DIV elements on top of each other?

Using CSS position property: The position: absolute; property is used to position any element at the absolute position and this property can be used to stack elements on top of each other. Using this, any element can be positioned anywhere regardless of the position of other elements.

How do you position an element based on another element?

The outer element should be a container element like divs or body tag itself. To place some element within the element the outer element has to occupy a larger width & height. Position properties are used to achieve this & make the elements to be placed by setting top, right, bottom & left properties.

How do you display an element on top of another?

If position: absolute; or position: fixed; - the top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor. If position: relative; - the top property makes the element's top edge to move above/below its normal position.


2 Answers

Maybe a negative margin?

header {
    width: 980px;
    height: 327px;
    background: url(../images/header.png);
}

nav {
    width: 980px;
    height: 180px;
    background: url(../images/menu.png);
    margin: -90px auto 0;
}

http://jsfiddle.net/NmUfT/

like image 81
Jared Farrish Avatar answered Oct 27 '22 09:10

Jared Farrish


Relative positioning could fix this for you:

nav {
  position: relative;
  top: -20px;
}
like image 32
Karl Barker Avatar answered Oct 27 '22 11:10

Karl Barker