简体   繁体   中英

z-index issue with two divs, one of them with absolute position

My question is very simple, I have two divs, one of them with absolute position. I set the z-index of the absolute position div to the lowest value and the other one with the highest value but always the one with absolute position remains on top (opposite behavior).

Here you have an image:

在此处输入图片说明

Here you have the code:

<div style="position:relative;">
    <div style="position:absolute;width:200px;background-color:#ff0;z-index:1;">
          SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK.
        SHOULD BE ON BACK. SHOULD BE ON BACK. 
    </div>
    <div style="width:300px;background-color:#0f0;z-index:999999;">
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
    </div>
</div>

Take a look at the following jsfiddle:

http://jsfiddle.net/rnbd3nek/

Add position: absolute; for "SHOULD BE ON FRONT" element

Note: z-index only effects elements that have a position value other than static (the default).

 <div style="position:relative;"> <div style="position:absolute;width:200px;background-color:#ff0;z-index:1;"> SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK. </div> <div style="width:300px;background-color:#0f0;z-index:999999;position:absolute;"> SHOULD BE ON FRONT. SHOULD BE ON FRONT. SHOULD BE ON FRONT. SHOULD BE ON FRONT. </div> </div>

Updated Fiddle

You need to get rid of the style in the parent div and add the style into the front div. Add position:relative to the 'THIS SHOULD BE ON FRONT'.

Thus:

<div>
    <div style="position:absolute;width:200px;background-color:#ff0;z-index:1;">
      SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK.
    SHOULD BE ON BACK. SHOULD BE ON BACK. 
    </div>
    <div style="position:relative;width:300px;background-color:#0f0;z-index:999999;">
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
    </div>
</div>

Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). -W3Schools, from here

Update your second div to

<div style="position:relative;width:300px;background-color:#0f0;z-index:999999;">
    SHOULD BE ON FRONT. SHOULD BE ON FRONT.
    SHOULD BE ON FRONT. SHOULD BE ON FRONT.
</div>

Z-index will only ever apply when the DIV has a position set (relative, absolute, fixed etc) - so both divs need a position, not just the one.

Your markup should be like this.

<div style="position:relative;">
    <div style="position:absolute;width:200px;background-color:#ff0;z-index:1;">
          SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK.
        SHOULD BE ON BACK. SHOULD BE ON BACK. 
    </div>
    <div style="position:relative;width:300px;background-color:#0f0;z-index:2;">
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
    </div>
</div>

Check this updated Fiddle

您可以将第一个 div 的 z-index 更改为 -1,或将position:relativeposition:absolute到第二个 div。

Here you go... Should be in front in not absolute and is in front :P

<div style="position:relative;">
  <div style="position:absolute;width:200px;background-color:red;z-index:1;">
    SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK.
  </div>
  <div style="width:300px;background-color:yellow;z-index:2;position: relative;">
    SHOULD BE ON FRONT. SHOULD BE ON FRONT. SHOULD BE ON FRONT. SHOULD BE ON FRONT.
  </div>
</div>

Make second div position absolute too..

<div style="position:absolute;width:300px;background-color:#0f0;z-index:999999;">

Final code will look like this

<div style="position:relative;">
    <div style="position:absolute;width:200px;background-color:#ff0;z-index:1;">
          SHOULD BE ON BACK. SHOULD BE ON BACK. SHOULD BE ON BACK.
        SHOULD BE ON BACK. SHOULD BE ON BACK. 
    </div>
    <div style="position:absolute;width:300px;background-color:#0f0;z-index:999999;">
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
        SHOULD BE ON FRONT. SHOULD BE ON FRONT.
    </div>
</div

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM