简体   繁体   English

CSS / HTML5:如何将div标签高度调整为等于浏览器窗口的当前高度?

[英]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 want to add vh so something like this. 您想添加vh像这样。

*{
  margin:0;
  padding:0;
}
.container{
  background:grey;
  height:100vh;
}

and HTML 和HTML

<div class="container">
  <h1>Hello from the other side</h1>
</div>

and here is an example . 这是一个例子

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> 

调整<div> Bootstrap 网站上的 -height 到 window-height</div><div id="text_translate"><p> 假设我有一个使用<strong>Bootstrap 4</strong>制作的简单网站,其中包含 header、导航栏、内容区域和页脚。<br> 我的演示网站可以在 JSFiddle 上找到并实时编辑: <a href="https://jsfiddle.net/26zda5xv/2/" rel="nofollow noreferrer">https://jsfiddle.net/26zda5xv/2/</a><br> 请在下面找到该网站的屏幕截图:</p><p> <a href="https://i.stack.imgur.com/aMvH9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aMvH9.png" alt="演示网站截图"></a></p><p> 我希望&lt;div class="container"&gt;至少填写整个页面/窗口高度。 ( min-height? )<br> 这应该通过扩展&lt;div id="content"&gt;的高度来完成。</p><p> 当然,该解决方案应该自动处理各种屏幕分辨率/设备。</p><p> 如何根据 JSFiddle 正确执行此操作?<br> Bootstrap 4 中是否有针对此任务的内置解决方案?</p><p> 谢谢!</p></div> - Adjust <div>-height to window-height on Bootstrap website

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML Div 等高 - HTML Div Equal Height div大小与浏览器窗口的高度和宽度 - Size div to browser's window height and width 如何使用CSS / Javascript根据窗口高度使div在两个div之间自动调整其高度? - How can I make a div auto-adjust its height between two divs, according to window height using CSS/Javascript? 如何获取浏览器窗口的高度,然后将 div 的高度设置为该值 - How to get height of a browser window and then set a height of a div to that value 如何根据浏览器大小调整div的高度 - How can I adjust the height of my div based on the browser size 设置div的高度,相对于浏览器窗口的高度 - Set height of div, relative to browser window height 如何使用jQuery或CSS自动调整(拉伸)div高度和宽度 - How to auto adjust (stretch) div height and width using jQuery or CSS CSS top div自动调整高度 - CSS top div to adjust height automatically 调整<div> Bootstrap 网站上的 -height 到 window-height</div><div id="text_translate"><p> 假设我有一个使用<strong>Bootstrap 4</strong>制作的简单网站,其中包含 header、导航栏、内容区域和页脚。<br> 我的演示网站可以在 JSFiddle 上找到并实时编辑: <a href="https://jsfiddle.net/26zda5xv/2/" rel="nofollow noreferrer">https://jsfiddle.net/26zda5xv/2/</a><br> 请在下面找到该网站的屏幕截图:</p><p> <a href="https://i.stack.imgur.com/aMvH9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aMvH9.png" alt="演示网站截图"></a></p><p> 我希望&lt;div class="container"&gt;至少填写整个页面/窗口高度。 ( min-height? )<br> 这应该通过扩展&lt;div id="content"&gt;的高度来完成。</p><p> 当然,该解决方案应该自动处理各种屏幕分辨率/设备。</p><p> 如何根据 JSFiddle 正确执行此操作?<br> Bootstrap 4 中是否有针对此任务的内置解决方案?</p><p> 谢谢!</p></div> - Adjust <div>-height to window-height on Bootstrap website 如何在窗口调整大小时为等高子项重新加载div - How to reload a div for equal height child on window resize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM