我想让主体有100%的浏览器高度。我可以用CSS来做吗?
我试着设置高度:100%,但它不起作用。
我想为页面设置一个背景色来填充整个浏览器窗口,但如果页面内容很少,我就会在底部得到一个丑陋的白色条。
我想让主体有100%的浏览器高度。我可以用CSS来做吗?
我试着设置高度:100%,但它不起作用。
我想为页面设置一个背景色来填充整个浏览器窗口,但如果页面内容很少,我就会在底部得到一个丑陋的白色条。
当前回答
只需要一行CSS就可以完成。
body{ height: 100vh; }
其他回答
请检查这个:
* {margin: 0; padding: 0;}
html, body { width: 100%; height: 100%;}
或者尝试新的方法Viewport height:
html, body { width: 100vw; height: 100vh;}
视窗: 如果你使用视口意味着任何大小的屏幕内容都将达到屏幕的全高。
html {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
min-height: 100%;
}
html body {
min-height: 100%
}
适用于所有主要浏览器:FF, Chrome, Opera, IE9+。工作与背景图像和梯度。滚动条可根据内容需要使用。
如果你想保持正文的边缘,不想要滚动条,使用下面的css:
html { height:100%; }
body { position:absolute; top:0; bottom:0; right:0; left:0; }
设置body {min-height:100%}将为您提供滚动条。
参见http://jsbin.com/aCaDahEK/2/edit?html,output上的演示。
所有的答案都是100%正确的,并且解释得很好, 但我做了一些很好的和非常简单的事情使它有响应。
这里元素将占用100%的视图高度,但当涉及到移动视图时,它看起来不太好,特别是在纵向视图(移动)上,所以当视图端口变得越来越小时,元素将会折叠并相互重叠。为了让它的响应更小,这里有代码。 希望有人能从中得到帮助。
<style>
.img_wrap{
width:100%;
background: #777;
}
.img_wrap img{
display: block;
width: 100px;
height: 100px;
padding: 50px 0px;
margin: 0 auto;
}
.img_wrap img:nth-child(2){
padding-top: 0;
}
</style>
<div class="img_wrap">
<img src="https://i.pinimg.com/originals/71/84/fc/7184fc63db0516a00e7d86900d957925.jpg" alt="">
<img src="https://i.pinimg.com/originals/71/84/fc/7184fc63db0516a00e7d86900d957925.jpg" alt="">
</div>
<script>
var windowHeight = $(window).height();
var elementHeight = $('.img_wrap').height();
if( elementHeight > windowHeight ){
$('.img_wrap').css({height:elementHeight});
}else{
$('.img_wrap').css({height:windowHeight});
}
</script>
这里是JSfiddle演示。
这里更新
html
{
height:100%;
}
body{
min-height: 100%;
position:absolute;
margin:0;
padding:0;
}