Placing absolute behind relative positioned element

匆匆过客 提交于 2019-12-19 05:02:06

问题


I have two elements in the same container, the first has position: absolute, the second position: relative. Is there a way to set the "z-index" of an absolute element so that its in the background? The relative positioned element should be on the top (z-layer) because it holds links...

Here is a JSFiddle: http://jsfiddle.net/8eLJz/

The absolute positioned element is on the top of the z-layer and I have no influence with the z-index-property. What can I do?


回答1:


I'm not sure which one you want in front, but you just need to set position on both and set z-index on both. http://jsfiddle.net/8eLJz/2/

a {
    color: black;
}

nav#mainNav {
    position: relative;
}

nav#mainNav > img {
    position: absolute;
    width: 100px;
    left: 0;
    z-index: 5;
}

nav#mainNav > a > img {
    width: 100%;
}

nav#mainNav > nav {
    width: 100%;
    position: relative;
    z-index: 10;
}

nav#mainNav > nav > a {
    display: block;
    text-align: right;
    background-color: yellow;
}



回答2:


http://jsfiddle.net/8eLJz/3/

nav#mainNav > img {
    z-index: -1;
}



回答3:


CSS has a z-index property so on your nav#mainNav > img selector just set z-index: -1;. Here is a working jsFiddle: http://jsfiddle.net/8eLJz/1/



来源:https://stackoverflow.com/questions/22151271/placing-absolute-behind-relative-positioned-element

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