我在页面的左侧有一个导航栏,我想让它伸展到页面高度的100%。不仅是视口的高度,还包括滚动之前隐藏的区域。我不想用javascript来完成这个。

它能在HTML/CSS中完成吗?


当前回答

我遇到了和你一样的问题。我想做一个DIV作为背景,为什么,因为它很容易通过javascript操作DIV。无论如何,我在css中为那个div做了三件事。

CSS:

{    
position:absolute; 
display:block; 
height:100%; 
width:100%; 
top:0px; 
left:0px; 
z-index:-1;    
}

其他回答

下面是我最终想出的解决方案,当使用div作为动态背景的容器时。

删除非后台使用的z索引。 删除全高列的左或右。 删除全宽行的顶部或底部。

编辑1:下面的CSS已经被编辑,因为它在FF和Chrome中没有正确显示。移动位置:相对于HTML上的位置,并将正文设置为height:100%而不是min-height:100%。

编辑2:添加额外的注释到CSS。上面增加了更多的说明。

CSS:

html{
    min-height:100%;/* make sure it is at least as tall as the viewport */
    position:relative;
}
body{
    height:100%; /* force the BODY element to match the height of the HTML element */
}
#cloud-container{
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    overflow:hidden;
    z-index:-1; /* Remove this line if it's not going to be a background! */
}

html:

<!doctype html>
<html>
<body>
    <div id="cloud-container"></div>
</body>
</html>

Why?

html{min-height:100%;position:relative;}

否则,云容器DIV将从HTML的布局上下文中移除。position: relative确保DIV在绘制时仍然在HTML框内,因此bottom:0指的是HTML框的底部。你也可以在云容器上使用height:100%,因为它现在指的是HTML标签的高度,而不是视口。

你可以使用伪列来作弊 或者您可以使用一些CSS技巧

我想在提示一个模态弹出窗口之前覆盖整个网页。我尝试了许多方法使用CSS和Javascript,但没有一个帮助,直到我想出以下解决方案。它对我有用,我希望它也能帮助到你。

<!DOCTYPE html> <html> <head> <style> html, body { margin: 0px 0px; height 100%; } div.full-page { position: fixed; top: 0; bottom: 0; width: 100%; height: 100%; background-color: #000; opacity:0.8; overflow-y: hidden; overflow-x: hidden; } div.full-page div.avoid-content-highlight { position: relative; width: 100%; height: 100%; } div.modal-popup { position: fixed; top: 20%; bottom: 20%; left: 30%; right: 30%; background-color: #FFF; border: 1px solid #000; } </style> <script> // Polling for the sake of my intern tests var interval = setInterval(function() { if(document.readyState === 'complete') { clearInterval(interval); isReady(); } }, 1000); function isReady() { document.getElementById('btn1').disabled = false; document.getElementById('btn2').disabled = false; // disable scrolling document.getElementsByTagName('body')[0].style.overflow = 'hidden'; } function promptModalPopup() { document.getElementById("div1").style.visibility = 'visible'; document.getElementById("div2").style.visibility = 'visible'; // disable scrolling document.getElementsByTagName('body')[0].style.overflow = 'hidden'; } function closeModalPopup() { document.getElementById("div2").style.visibility = 'hidden'; document.getElementById("div1").style.visibility = 'hidden'; // enable scrolling document.getElementsByTagName('body')[0].style.overflow = 'scroll'; } </script> </head> <body id="body"> <div id="div1" class="full-page"> <div class="avoid-content-highlight"> </div> </div> <button id="btn1" onclick="promptModalPopup()" disabled>Prompt Modal Popup</button> <div id="demo"> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> <h2>Original content</h2> </div> <div id="div2" class="modal-popup"> I am on top of all other containers <button id="btn2" onclick="closeModalPopup()" disabled>Close</button> <div> </body> </html>

祝你好运;-)

在HTML5中,最简单的方法就是设置高度:100vh。其中“vh”代表浏览器窗口的视口高度。响应浏览器和移动设备的大小调整。

我也遇到过类似的问题,解决方法是这样的:

#cloud-container{
    position:absolute;
    top:0;
    bottom:0;
}

我想要一个以页面为中心的div高度100%的页面高度,所以我的总解决方案是:

#cloud-container{
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0; 
    width: XXXpx; /*otherwise div defaults to page width*/
    margin: 0 auto; /*horizontally centers div*/
}

你可能需要让一个父元素(或简单的“body”)具有位置:relative;