【问题标题】:z-index not working with absolute block on relative blockz-index 不适用于相对块上的绝对块
【发布时间】:2013-07-13 23:37:40
【问题描述】:

我试图通过单击一个元素将子菜单显示在前面。子菜单(蓝色)应该覆盖下面的导航菜单(黄色),但它没有。

这是一个带有损坏行为的演示:http://jsfiddle.net/rqeRC/1/

sub menu == #profileMenu
navigation menu == #navigation

子菜单是 z-index 3 的绝对定位块,而导航菜单是 z-index 2 的相对定位块。似乎不能这样工作。

注意:由于后台已经有固定元素,我确实需要 z-index 将这些元素保留在前台。

【问题讨论】:

    标签: css z-index


    【解决方案1】:

    只需使 #profile 上的 z-index 高于 #navigation 上的 z-index。例如

    #profile {
        float: right;
        margin: 60px 47px 0px 0px;
        position: relative;
        z-index: 20;
    }
    

    【讨论】:

    • 哇,我没有意识到#profileMenu 在#profile 中。所以父级的 z-index 用于结合相对/绝对进行渲染。
    【解决方案2】:

    navigation 和#profileMenu 的z-index 相同,为2,请将profileMenu 的z-index 设置为更高的值。

    小提琴:http://jsfiddle.net/rqeRC/2/

    #profile {
        float: right;
        margin: 60px 47px 0px 0px;
        position: relative;
        z-index: 3;
    }
    #navigation {
        float: right;
        margin: 60px 47px 0px 0px;
        min-height: 248px;
        position: relative;
        width: 248px;
        z-index: 2;
    }
    

    【讨论】:

    • 您混淆了#profileMenu 和#profile,但是是的,我也这样做了。 ^^
    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2013-01-07
    • 2014-04-06
    • 2017-09-29
    • 1970-01-01
    • 2022-10-17
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多