简体   繁体   English

z-index 和 position fixed 如何在 css 中协同工作

[英]How does z-index and position fixed work together in css

 body { height: 3000px; } .fixed { position: fixed; width: 100%; height: 60px; top: 0; left: 0; background: #f4f4f4; } .fixed h3 { position: relative; z-index: 300; color: #fff; } .overlay { background-color: #000; top: 0; bottom: 0; left: 0; right: 0; opacity: .5; position: fixed; z-index: 1; -webkit-transition: all 0.3s ease-out; -moz-transition: all 0.3s ease-out; -ms-transition: all 0.3s ease-out; -o-transition: all 0.3s ease-out; transition: all 0.3s ease-out; }
 <div class="overlay"></div> <div class="fixed"> <center> <h3> Only this thing should be brought on top of overlay.. </h3> </center> </div>

Now how do I place get the h3 inside the .fixed div on top of overlay.现在我如何将 h3 放置在 .fixed div 中的叠加层之上。 Please post some reading on fixed along with z-index.请与 z-index 一起发布一些关于 fixed 的阅读。

http://jsfiddle.net/CvMLw/4/ Check the above jsfiddle, So how do i bring the h3 on top of overlay.. http://jsfiddle.net/CvMLw/4/检查上面的 jsfiddle,那么我如何将 h3 放在叠加层之上..

You set a z-index on an element inside a fixed div that is under the overlay.您在覆盖层下的固定div内的元素上设置z-index

In other words, the overlay hiding the z-index value is 301.换句话说,隐藏z-index值的叠加层是 301。

You see, it's like lego blocks.你看,它就像乐高积木。 .fixed is at the bottom with its z-index at 0. .fixed位于底部,其 z-index 为 0。

Then on .fixed your h3 is 300 blocks one over another.然后在.fixed你的h3是 300 个块。

If we add, for example, another div below the h3 tag with z-index of 150, the "tower of blocks" would be lower than the h3 so h3 would be on the top.例如,如果我们在z-index为 150 的h3标签下方添加另一个div ,“方块塔”将低于h3因此h3将位于顶部。

Then there is another div ( .overlay ) on the same DOM level as .fixed with a higher z-index than .fixed .然后还有另外一个div.overlay在同一水平DOM)作为.fixed具有较高z-index.fixed This block would placed right over the 300 blocks of h3 .这个块将放置在h3的 300 个块上。

For example:例如:

<==============================>   <- the .overlay (z-index 1)
            <=>
            <=>
            <=>
            <=>
  <=>       <=>                <- Elements inside .fixed (random z-index)
  <=>       <=>
  <=>       <=>
  <=>       <=>
  <=>       <=>
<==============================>    <- the .fixed (z-index 0)

To set the h3 on top, put it on the same lvl of your overlay or set a higher .fixed z-index .要将 h3 设置在顶部,请将其放在叠加层的相同 lvl 上或设置更高的.fixed z-index

You can't do that if .fixed element is acting as a holder for any of its children.如果.fixed元素充当其.fixed元素的持有者,则不能这样做。

This means that if, for example, you set your h3 with an absolute position to the bottom, it will go to the bottom of its parent element .这意味着,例如,如果您将h3的绝对位置设置为底部,它将转到其父元素的底部

z-index works the same way. z-index工作方式相同。 Its value will be inherited by the parent.它的值将由父级继承。 Since the z-index default value is 0 , your .fixed element has a z-index value of 0 , and your h3 has first 0 and then 300 .由于z-index默认值为0 ,因此您的.fixed元素的z-index值为0 ,您的h3首先是0 ,然后是300

Change your改变你的

position:fixed; 

to

position:absolute; 

on .fixed , then it works..fixed ,然后它就可以工作了。

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

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