为什么我的叠加层覆盖了一些元素但不是全部?

时间:2015-04-30 15:37:01

标签: javascript html css

我尝试使用div创建自己的模态对话框:

html:
<div id="overlay"> </div>    
<div id="popup">  

    <input type="submit"/>
    <p>Some text</p>

</div>

css:
#overlay{
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0px;
    top: 0px;
    background-color:#000;
    opacity: .75;
    z-index: -1;
}

#popup {
    position: absolute;
    text-align:center;
    z-index: 1000;
}

http://jsfiddle.net/wfsaxton/fq7mhefa/

我不确定原因,但我的叠加层不仅覆盖了页面,还覆盖了部分弹出窗口(背景和文字)。我只希望它在后台覆盖页面......弹出窗口应该完全位于叠加层之上。

有什么想法可能会发生什么?

3 个答案:

答案 0 :(得分:3)

&#13;
&#13;
#overlay{
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0px;
    top: 0px;
    background-color:#000;
    opacity: .75;
    z-index: 5;
}

.popup {
    display: none;
    position: absolute;
    text-align:center;
    z-index: ;
}
&#13;
<div>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div>

    <div id="underlay">  
        <input type="submit"/>
        <p>Some text</p>
    </div>

<div id="overlay">  
    <div id="popup">  
        <input type="submit"/>
        <p>Some text</p>
    </div>
</div>
&#13;
&#13;
&#13;

您将叠加层设置为-1,这意味着它低于默认的z-index:0几乎每个html元素。

答案 1 :(得分:2)

覆盖所有内容,z-index值必须大于其他所有内容。你的-1

答案 2 :(得分:0)

呃,这太简单了。我的#popup没有分配背景,所以我猜它默认是透明的。

刚补充说:

#popup {
    ....
    background-color: white;
}

它现在有效:http://jsfiddle.net/wfsaxton/fq7mhefa/12/