简体   繁体   English

两个 div 的 z-index 问题,其中一个具有绝对位置

[英]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.我的问题很简单,我有两个 div,其中一个具有绝对位置。 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).我将绝对位置 div 的 z-index 设置为最低值,将另一个值设置为最高值,但始终将绝对位置的那个保持在顶部(相反的行为)。

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:看看下面的jsfiddle:

http://jsfiddle.net/rnbd3nek/ http://jsfiddle.net/rnbd3nek/

Add position: absolute;添加position: absolute; for "SHOULD BE ON FRONT" element对于“应该在前面”元素

Note: z-index only effects elements that have a position value other than static (the default).注意: z-index只影响position值不是static (默认)的元素。

 <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.您需要摆脱父div中的样式并将样式添加到前面的div中。 Add position:relative to the 'THIS SHOULD BE ON FRONT'.添加position:relative对于“这应该在前面”。

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).注意:z-index 仅适用于定位元素(位置:绝对、位置:相对或位置:固定)。 -W3Schools, from here -W3Schools,从这里开始

Update your second div to将您的第二个 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>

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. Z-index 仅在 DIV 具有位置集(相对、绝对、固定等)时才适用 - 因此两个 div 都需要一个位置,而不仅仅是一个位置。

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给你......应该在前面而不是绝对的,在前面: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 位置也绝对..

<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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