绝对与固定定位

时间:2013-07-04 18:59:45

标签: html css css3

我有一个自定义对话框,单击按钮时会显示该对话框。显示对话框后,我显示了一个叠加层。叠加层的高度和宽度为100% x 100%。这就出现了问题,高度100%只是获得了浏览器窗口的高度,所以当我向下滚动页面时,它仍然位于顶部。如何将其高度设置为整页高度而不是浏览器?

Fiddle.

HTML:

<div id="overlay"></div>
<div class="description" style="text-align: justify;">Some text..(whole big text is in the fiddle didn't wrote here to shorten the code :))</div>
<div style="text-align: right">
    <button id="offer_help">Offer Help</button>
</div>
<div class="offer_a_help">
    <textarea rows="5">Write a short experience about yourself</textarea>
    <textarea rows="5">Write what do you want in return</textarea>
    <button id="send_offer">Send Offer</button>
</div>

CSS:

#overlay {
    opacity: 0.5;
    width: 100%;
    height: 100%;
    background-color: black;
    display: none;
    position: absolute;
    top: 0;
    left: 0;
}
#offer_help {
    background-color: #eee;
    border: 0;
    padding: 10px;
    border-radius: 2px;
    box-shadow: 0px 0px 3px 1px #aaa;
}
.offer_a_help {
    display: none;
    width: 400px;
    height: 250px;
    position: fixed;
    top: calc(100%/2 - 350px/2);
    left: calc(100%/2 - 250px/2);
    background-color: #eee;
    border: 1px solid #bbb;
    text-align: center;
}
.offer_a_help textarea {
    width: 90%;
    padding: 2px;
    font-family: Calibri;
}
.offer_a_help textarea:first-child {
    margin-top: 20px;
}
.offer_a_help button {
    float: right;
    margin-top: 10px;
    margin-right: 10px;
    border: 1px solid #bbb;
    background-color: #ddd;
    padding: 5px;
    border-radius: 3px;
}

如何将其高度设置为整页高度而不是浏览器?

2 个答案:

答案 0 :(得分:3)

position: absolute使元素与文档不一致。所以高度是视口的高度,顶部,左边的值是静态的。将其更改为position: fixed,您会看到更好的结果。

答案 1 :(得分:0)

使用位置:固定。

http://jsfiddle.net/ryJEW/2/

#overlay {
opacity: 0.5;
width: 100%;
height: 100%;
background-color: black;
display: none;
position: fixed;
top: 0;
left: 0;
}