z-index not working properly

2019-03-03 17:12发布

So I am making a site and I have a top bar with some box shadow. I then have a description box directly underneath. So I set the z-indexes to ensure that the top bars box-shadow would go over the description box with the following css:

#topbar{
    z-index:9999 !important;
}
#description{
    margin-top:0px;
    z-index:-1 !important;
}

jsfiddle

you will notice if you change the margin-top, you can see that the shadow is being hidden underneath the description still!

why is this happening and how can I fix it?

note: neither element can have fixed or absolute positioning

标签: html css z-index
1条回答
狗以群分
2楼-- · 2019-03-03 17:50

Add position: relative to both elements. z-index only affects positioned elements.

Updated JSFiddle: http://jsfiddle.net/BAW23/3/

查看更多
登录 后发表回答