Why z-index is not working for div?

时光毁灭记忆、已成空白 提交于 2019-12-03 11:25:18

问题


I am trying to get my footer to show on top of the footer background, but z-index seems not to be working. Does anyone see what is wrong with it? http://jsfiddle.net/f2ySC/


回答1:


You have to explicitly define the position property:

.footerBox {
    background-color: #FFFFFF;
    border: 10px solid #DDDDDD;
    margin: 10px 0 -200px -10px;
    width: 1185px;
    z-index: 1000;

    position:relative;

}

http://jsfiddle.net/f2ySC/1/


This brings the footer into the current stacking context:

... The root element forms the root stacking context. Other stacking contexts are generated by any positioned element (including relatively positioned elements) having a computed value of 'z-index' other than 'auto'. Stacking contexts are not necessarily related to containing blocks. In future levels of CSS, other properties may introduce stacking contexts, for example 'opacity' ...

http://www.w3.org/TR/CSS2/visuren.html#z-index




回答2:


using negative margin is not a good practice. z-index:-1; works it just display the content on the background of another div :)




回答3:


Is this the effect you are looking for? Updated example

I added position: relative; to #footerBack and .footerBox to enable z-index




回答4:


On footerBox set the position attribute to absolute Your new code should read as follows

.footerBox {
    background-color: #FFFFFF;
    border: 10px solid #DDDDDD;
    margin: 10px 0 -200px -10px;
    width: 1185px;
    z-index: 1000;
    position: absolute;
}

See a demo here

Using position: relative also works



来源:https://stackoverflow.com/questions/9592323/why-z-index-is-not-working-for-div

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