Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing absolute behind relative positioned element

I have two elements in the same container, the first has position: absolute, the second position: relative. Is there a way to set the "z-index" of an absolute element so that its in the background? The relative positioned element should be on the top (z-layer) because it holds links...

Here is a JSFiddle: http://jsfiddle.net/8eLJz/

The absolute positioned element is on the top of the z-layer and I have no influence with the z-index-property. What can I do?

like image 819
Luca Nate Mahler Avatar asked Mar 03 '14 15:03

Luca Nate Mahler


People also ask

How do you position position absolute and relative?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Can an element have position absolute and relative?

If you position it as absolute , then you're positioning it relative to its closest ancestor which is fixed or relative ... Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.

How do you position an element with an absolute position?

In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.

Does position Absolute is kept relative to viewport?

Whereas the position and dimensions of an element with position:absolute are relative to its containing block, the position and dimensions of an element with position:fixed are always relative to the initial containing block. This is normally the viewport: the browser window or the paper's page box.


1 Answers

I'm not sure which one you want in front, but you just need to set position on both and set z-index on both. http://jsfiddle.net/8eLJz/2/

a {     color: black; }  nav#mainNav {     position: relative; }  nav#mainNav > img {     position: absolute;     width: 100px;     left: 0;     z-index: 5; }  nav#mainNav > a > img {     width: 100%; }  nav#mainNav > nav {     width: 100%;     position: relative;     z-index: 10; }  nav#mainNav > nav > a {     display: block;     text-align: right;     background-color: yellow; } 
like image 131
2pha Avatar answered Oct 04 '22 06:10

2pha