[英]CSS/HTML5: How to adjust div tag height equal to browser window's current height?
I want the height of my div tag exactly equal to the height of the browser's window. 我希望div标签的高度完全等于浏览器窗口的高度。 So if you resize the browser the should adjust automatically. 因此,如果您调整浏览器的大小,应该会自动调整。 So what should I set my height equal to in CSS. 所以我应该将我的身高设置成等于CSS中的高度。
I have tried 我努力了
.class {
height: 100%;
}
Doesn't work. 不起作用
em, rem, vh, wh apparently only work with the font. em,rem,vh,wh显然只能使用该字体。
If I however, hard-code the body tag to some amount of pixels, then the percentage works for the children tags but this is not dynamic. 但是,如果我将body标签硬编码为一定数量的像素,则该百分比适用于child标签,但这不是动态的。
What I want: Just as I am viewing this stack page in my browser. 我想要什么:就像我在浏览器中查看此堆栈页面一样。 I wish when I resize the browser I still get the same amount of page viewership, only resized for the new size (responsive). 我希望当我调整浏览器的大小时,我仍能得到相同数量的页面观看量,仅将其大小调整为新的大小(响应)。
Thanks. 谢谢。
You need to add 您需要添加
html, body {
width: 100%;
margin: 0;
}
to set a height reference for the DIV (any DIV is a child of another element, a DIV without another container is a child of body
, which is a child of html
) 设置DIV的高度参考(任何DIV是另一个元素的子代,没有另一个容器的DIV是body
的子代,它是html
的子代)
Your divs parent is the body. 您的divs父母就是身体。 height:100%
will go as high as the parent. height:100%
将与父级一样高。 So you need to do this. 因此,您需要执行此操作。
html,body {
height:100%;
margin:0;
}
You can do it by jquery on simple way. 您可以通过jquery以简单的方式完成此操作。
$(document).ready(function(){
var heights = window.innerHeight;
$(".class ").style.height = heights + "px";
});
Yes you can add VH height to a div and completely agree with @Nofel. 是的,您可以将VH高度添加到div并完全同意@Nofel。
But it will will for mozilla and chrome. 但是它将适用于mozilla和chrome。 It will not work for Safari or IE. 它不适用于Safari或IE。
Add vh property resize the window. 添加vh属性可调整窗口大小。
You can bind a function to resize the container on window resize. 您可以绑定一个函数以在窗口调整大小时调整容器的大小。
HTML: HTML:
<div class="container">
Content
</div>
CSS: CSS:
.container {
height: 100vh;
}
JavaScript: JavaScript:
function resize_container() {
var height = $(window).height(),
container = $('.container');
container.css('height', height);
}
$(window).resize(resize_container);
Solution 1 解决方案1
You can try height :100vh
to achieve this. 您可以尝试height :100vh
来实现。 Check below snippet: 检查以下代码段:
body{ margin: 0; } .windowHeight{ height: 100vh; display: block; background: #000; }
<div class="windowHeight"> </div>
Solution 2 解决方案2
Another way to do this by jQuery. jQuery的另一种方法。 Check below snippet: 检查以下代码段:
$('.windowHeight').css('height', $(window).height());
body{ margin:0; } .windowHeight{ background:#000; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="windowHeight"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.