Kny*_*yri 166
这是我在使用div作为动态背景的容器时最终想出的解决方案.
z-index
非背景用途.left
或right
使用全高柱.top
或bottom
全宽行.编辑1:下面的CSS已被编辑,因为它在FF和Chrome中无法正确显示.移动position:relative
到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! */
}
Run Code Online (Sandbox Code Playgroud)
html:
<!doctype html>
<html>
<body>
<div id="cloud-container"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么?
html{min-height:100%;position:relative;}
Run Code Online (Sandbox Code Playgroud)
如果没有这个,云容器DIV将从HTML的布局上下文中删除.position: relative
确保DIV在绘制时保留在HTML框内,以便bottom:0
引用HTML框的底部.您也可以height:100%
在云容器上使用它,因为它现在指的是HTML标记的高度而不是视口.
alc*_*ang 70
使用HTML5,最简单的方法就是做height: 100vh
.其中'vh'代表浏览器窗口的视口高度.响应浏览器和移动设备的大小调整.
Nat*_*arr 12
我有类似的问题,解决方案是这样做:
#cloud-container{
position:absolute;
top:0;
bottom:0;
}
Run Code Online (Sandbox Code Playgroud)
我想要一个以页面为中心的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*/
}
Run Code Online (Sandbox Code Playgroud)
您可能需要创建父元素(或简称"body") position: relative;
使用表格很简单:
<html>
<head><title>100% Height test</title></head>
<body>
<table style="float: left; height: 100%; width: 200px; border: 1px solid red">
<tbody><tr><td>Nav area</td></tr></tbody>
</table>
<div style="border: 1px solid green;">Content blabla...
text<br />
text<br />
text<br />
text<br />
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当DIV被引入时,人们如此害怕桌子,穷人的DIV成为了隐喻的锤子.
小智 7
使用绝对位置.请注意,这不是我们通常习惯使用绝对位置的方式,这需要手动布局或具有浮动对话框.当您调整窗口或内容的大小时,这将自动拉伸.我认为这需要标准模式,但可以在IE6及更高版本中使用.
只需将id为'thecontent'的div替换为您的内容(指定的高度仅用于说明,您不必在实际内容上指定高度.
<div style="position: relative; width: 100%;">
<div style="position: absolute; left: 0px; right: 33%; bottom: 0px; top: 0px; background-color: blue; width: 33%;" id="navbar">nav bar</div>
<div style="position: relative; left: 33%; width: 66%; background-color: yellow;" id="content">
<div style="height: 10000px;" id="thecontent"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这种方式的工作方式是外部div充当导航栏的参考点.外部div由"content"div的内容延伸.导航栏使用绝对定位将其自身伸展到其父级的高度.对于水平对齐,我们使内容div自身偏移导航栏的相同宽度.
使用CSS3 flex box模型可以轻松实现这一点,但是IE尚未提供,并且有一些自己的怪癖.
小智 5
我遇到了和你一样的问题.我想制作一个DIV
背景,为什么,因为它很容易通过javascript操纵div.无论如何我在css中为那个div做了三件事.
CSS:
{
position:absolute;
display:block;
height:100%;
width:100%;
top:0px;
left:0px;
z-index:-1;
}
Run Code Online (Sandbox Code Playgroud)
我想在提示模式弹出窗口之前覆盖整个网页。我尝试了许多使用 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>
Run Code Online (Sandbox Code Playgroud)
祝你好运 ;-)
如果您瞄准的是更现代的浏览器,那么生活可能非常简单。尝试:
.elem{
height: 100vh;
}
Run Code Online (Sandbox Code Playgroud)
如果您在页面的50%处需要它,请将100替换为50。
归档时间: |
|
查看次数: |
328194 次 |
最近记录: |