[html] CSS z-index not working (position absolute)

I am trying to make the black div (relative) above the second yellow one (absolute). The black div's parent has a position absolute, too.

_x000D_
_x000D_
#relative {_x000D_
 position: relative;_x000D_
 width: 40px;_x000D_
 height: 100px;_x000D_
 background: #000;_x000D_
 z-index: 1;_x000D_
 margin-top: 30px;_x000D_
}_x000D_
.absolute {_x000D_
 position: absolute;_x000D_
 top: 0; left: 0;_x000D_
 width: 200px;_x000D_
 height: 50px;_x000D_
 background: yellow;_x000D_
 z-index: 0;_x000D_
}
_x000D_
<div class="absolute">_x000D_
    <div id="relative"></div>_x000D_
</div>_x000D_
<div class="absolute" style="top: 54px"></div>
_x000D_
_x000D_
_x000D_

Expected Result:

enter image description here

This question is related to html css css-position absolute

The answer is


Just add the second .absolute div before the other .second div:

<div class="absolute" style="top: 54px"></div>
<div class="absolute">
    <div id="relative"></div>
</div>

Because the two elements have an index 0.


How about this?

http://jsfiddle.net/P7c9q/4/

<div class="relative">
  <div class="yellow-div"></div>
  <div class="yellow-div"></div>
  <div class="absolute"></div>
</div>

.relative{
position:relative;
}

.absolute {
position:absolute;
width: 40px;
height: 100px;
background: #000;
z-index: 1;
top:30px;
left:0px;
}
.yellow-div {
position:relative;
width: 200px;
height: 50px;
background: yellow;
margin-bottom:4px;
z-index:0;
}

use the relative div as wrapper and let the yellow div's have normal positioning.

Only the black block need to have an absolute position then.


I was struggling to figure it out how to put a div over an image like this: z-index working

No matter how I configured z-index in both divs (the image wrapper) and the section I was getting this:

z-index Not working

Turns out I hadn't set up the background of the section to be background: white;

so basically it's like this:

<div class="img-wrp">
  <img src="myimage.svg"/>
</div>
<section>
 <other content>
</section>

section{
  position: relative;
  background: white; /* THIS IS THE IMPORTANT PART NOT TO FORGET */
}
.img-wrp{
  position: absolute;
  z-index: -1; /* also worked with 0 but just to be sure */
}

JSFiddle

You have to put the second div on top of the first one because the both have an z-index of zero so that the order in the dom will decide which is on top. This also affects the relative positioned div because its z-index relates to elements inside the parent div.

<div class="absolute" style="top: 54px"></div>
<div class="absolute">
    <div id="relative"></div>
</div>

Css stays the same.


try this code:

.absolute {
    position: absolute;
    top: 0; left: 0;
    width: 200px;
    height: 50px;
    background: yellow;

}

http://jsfiddle.net/manojmcet/ks7ds/


I solved my z-index problem by making the body wrapper z-index:-1 and the body z-index:-2, and the other divs z-index:1.

And then the later divs didn't work unless I had z-index 200+. Even though I had position:relative on each element, with the body at default z-index it wouldn't work.

Hope this helps somebody.


I was struggling with this problem, and I learned (thanks to this post) that:

opacity can also affect the z-index

_x000D_
_x000D_
div:first-child {_x000D_
  opacity: .99; _x000D_
}_x000D_
_x000D_
.red, .green, .blue {_x000D_
  position: absolute;_x000D_
  width: 100px;_x000D_
  color: white;_x000D_
  line-height: 100px;_x000D_
  text-align: center;_x000D_
}_x000D_
_x000D_
.red {_x000D_
  z-index: 1;_x000D_
  top: 20px;_x000D_
  left: 20px;_x000D_
  background: red;_x000D_
}_x000D_
_x000D_
.green {_x000D_
  top: 60px;_x000D_
  left: 60px;_x000D_
  background: green;_x000D_
}_x000D_
_x000D_
.blue {_x000D_
  top: 100px;_x000D_
  left: 100px;_x000D_
  background: blue;_x000D_
}
_x000D_
<div>_x000D_
  <span class="red">Red</span>_x000D_
</div>_x000D_
<div>_x000D_
  <span class="green">Green</span>_x000D_
</div>_x000D_
<div>_x000D_
  <span class="blue">Blue</span>_x000D_
</div>
_x000D_
_x000D_
_x000D_


This is because of the Stacking Context, setting a z-index will make it apply to all children as well.

You could make the two <div>s siblings instead of descendants.

<div class="absolute"></div>
<div id="relative"></div>

http://jsfiddle.net/P7c9q/3/


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to css-position

How does the "position: sticky;" property work? How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3? CSS z-index not working (position absolute) Relative div height How can I make the contents of a fixed element scrollable only when it exceeds the height of the viewport? How to position the Button exactly in CSS How to make fixed header table inside scrollable div? Absolute positioning ignoring padding of parent Have a fixed position div that needs to scroll if content overflows How to make div fixed after you scroll to that div?

Examples related to absolute

React Native absolute positioning horizontal centre How to center div vertically inside of absolutely positioned parent div CSS z-index not working (position absolute) How to center absolute div horizontally using CSS? Set height 100% on absolute div Position: absolute and parent height? How to place a div on the right side with absolute position insert vertical divider line between two nested divs, not full height How to retrieve absolute path given relative Center an element with "absolute" position and undefined width in CSS?