The z-index property is not behaving as expected

半城伤御伤魂 提交于 2019-11-28 14:25:56

An element needs to be positioned in order for the z-index to work. See the visual formatting modal.

In this instance, you could simply add position:relative to the box.

UPDATED EXAMPLE HERE

.welcome {
    z-index:10;
    position: relative;
    background: #fff;
    margin: 0 auto;
    width: 200px;
    padding: 100px;
    text-align: center;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    -webkit-box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 3px;
    -moz-box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 3px;
    box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 3px;
}

No need for !important either.

Pinki

In order to use z-index, your <div>s can't be position: static; (which is the default)

So in order to display the .welcome above the hearts you need to add position:relative;

Then it should work.

Your z-index is not working because your .welcome box has a static positioning, so your

.welcome { z-index: 10 !important } 

declaration is not having any effect. Because of this, the hearts, that have z-index:0, will still be on top.

To fix this, either

a) set the hearts to z-index: -1

.heart { z-index: -1 }

jsFiddle

b) set .welcome's position to something different than it's initial static position.

.welcome { position: relative }

jsFiddle

And lose the !important, that's bad practice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!