简体   繁体   English

如何让 div 在浏览器中占据 100% 的视口高度

[英]How to make div occupy 100% of viewport height in browser

I have a div tag and I realized that it is not filling 100% of the height as it should.我有一个 div 标签,我意识到它没有按应有的方式填充 100% 的高度。

My code:我的代码:

 #container { width: 100vw; height: 100vh; background: purple; } body { margin: 0px; } <div id="container"></div>
 <div id="container"></div>

What's happening?发生了什么?

Well, if I only have this code snippet above that I put the div to occupy 100% of the viewport of course!好吧,如果我只有上面的这段代码,我当然会把 div 占据 100% 的视口! The problem is that I don't only have this code on my page, I have more things and I inserted this code section inside the body tag, a certain" place "inside the body tag, that is, I inserted some elements, and after these elements have this div, but it does not occupy, 100% of the viewport observe how it is问题是我的页面不仅有这段代码,我还有更多的东西,我在body标签里面插入了这段代码,body标签里面的某个“地方”,也就是我插入了一些元素,并且这些元素有这个div之后,但是不占,100%的视口观察是怎么回事

How is the result visually on your page?结果在您的页面上视觉效果如何?

在此处输入图像描述

I scrolled the page, but my div was still to occupy 100% of the entire viewport.我滚动了页面,但我的 div 仍然占据了整个视口的 100%。 Am I not correct?我不正确吗? and if this was supposed to happen why isn't it happening?如果这应该发生,为什么没有发生?

Explanation: Guys, I discovered the problem but I don't know how to solve it, well, here's the thing, the div does not occupy 100% of the viewport when it has an element below the container div or above, look at this image and see:解释:各位,我发现了问题,但是不知道怎么解决,好吧,事情是这样的,当它有一个低于容器div或高于容器的元素时,div不会占据100%的视口,看看这个图像并查看:

在此处输入图像描述

And the code I used that made this happen:我使用的代码实现了这一点:

My code HTML:我的代码 HTML:

 body { color: red; background: green; margin: 0; padding: 0; } #container { width: 100vw; height: 100vh; z-index: 1; background: purple; } p { font-size: 20pt; }
 <div id="container"></div> <p>ksksks</p>

What happens in the image above is the same problem that happens on my page, that is, the div only fills 100% of the viewport's height if it has no elements on the page, and I want to be able to make the height 100% of the viewport even with elements on the page.上图中发生的问题与我的页面上发生的问题相同,即如果页面上没有元素,div 只会填充视口高度的 100%,我希望能够使高度为 100%即使在页面上有元素的视口。

Edit:编辑:

Well apparently I saw that there are a lot of answers, and most of them don't work or are explaining the error or presenting answers that don't solve the problem, other people suggested using position fixed which in fact solves the problem, but I don't want to have to do it this way, think that you have a chat because you would want it to have a scrolling bullet and it will be the chat that will occupy the entire viewport and not another div understand?好吧,显然我看到有很多答案,其中大多数都不起作用,或者正在解释错误或提供无法解决问题的答案,其他人建议使用 position fixed 这实际上解决了问题,但是我不想这样做,认为你有一个聊天,因为你希望它有一个滚动的项目符号,它将占据整个视口而不是另一个 div 理解? This solution actually solves the problem, but I don't like jerry-rig.这个解决方案实际上解决了问题,但我不喜欢 jerry-rig。

I would like to know a more elegant way of doing it, for example my div container occupies 100% of the viewport but I don't want other elements to appear, I want the div container to overlap any element that should appear and I don't want to scroll the page.我想知道一种更优雅的方法,例如我的 div 容器占据了 100% 的视口,但我不希望其他元素出现,我希望 div 容器与任何应该出现的元素重叠,我不不想滚动页面。

Summary:概括:

To summarize everything in a few words, the div should occupy 100% of the viewport and make sure that the body does not have a scroll and the page goes to the top, that is, regardless of which position on the page the user is in, I want the page to go to the top and disable the scroll, and without javascript preferably, I don't want to write too much javascript being possible to write in html and css:) I will take advantage of the reward in this answer to add this and ask for a solution to this problem.一言以蔽之,div要占据100%的视口,并确保body没有滚动,页面到顶部,即不管用户在哪个页面position , I want the page to go to the top and disable the scroll, and without javascript preferably, I don't want to write too much javascript being possible to write in html and css:) I will take advantage of the reward in this answer添加此内容并寻求解决此问题的方法。

The problem has to do with the fact that the vh and vw units don't take the (added) scrollbar width/height into consideration.问题与vhvw单位没有考虑(添加的)滚动条宽度/高度的事实有关。 As long as the page isn't higher than the viewport, no scrollbar appears and 100vh will be exactly the height of the viewport and everything works as expected.只要页面不高于视口,就不会出现滚动条,并且 100vh 将恰好是视口的高度,并且一切都按预期工作。

But as soon as there is more content below or above, a vertical scrollbar appears: Now the width: 100vw is wider than the window width minus the vertical scrollbar, so a horizontal scrollbar appears, and now the height: 100vh is higher than the window height minus the (horizontal) scrollbar.但是只要下面或上面有更多内容,就会出现垂直滚动条: 现在width: 100vwwindow 宽度减去垂直滚动条宽,所以出现水平滚动条,现在height: 100vh比 window 高高度减去(水平)滚动条。

I consider that a kind of bug, but that's the way it is - in most browsers, it seems.我认为这是一种错误,但事实就是如此——在大多数浏览器中,似乎如此。 I posted this question a long time ago which basically covers the same issue: Problem using vw units when a vertical scrollbar appears (full-width elements in WordPress)我很久以前发布了这个问题,它基本上涵盖了相同的问题: Problem using vw units when a vertical scrollbar appears (full-width elements in WordPress)

Addition/edit after comments:评论后添加/编辑:

There is no 100% safe solution, I would say.我想说,没有 100% 安全的解决方案。 But one thing that helps to some extent is to not use 100vw for the width , but instead 100% , which does consider the (vertical) scrollbar.但是在某种程度上有帮助的一件事是不要使用100vw作为width ,而是使用100% ,这确实考虑了(垂直)滚动条。 However, width: 100%;但是, width: 100%; is the default for any block element anyway, so you can simply erase the width setting and only use height: 100vh , which will work (ie have the exact viewport height) as long as you don't have any special width requirements.无论如何,它是任何块元素的默认值,因此您可以简单地删除width设置并仅使用height: 100vh ,只要您没有任何特殊的宽度要求,它将起作用(即具有确切的视口高度)。

Please use the following meta tag inside your head tag请在您的 head 标签内使用以下元标签

<meta name="viewport" content="width=device-width, initial-scale=1">

 <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { color: red; background: green; margin: 0; padding: 0; } #container { width: 100vw; height: 100vh; z-index: 1; background: purple; } p { font-size: 20pt; } </style> </head> <body> <div id="container"></div> <p>ksksks</p> </body> </html>

Based On your question you want the div to take up the space instead of the purple background right?根据您的问题,您希望 div 占用空间而不是紫色背景,对吗?

Css: Css:

#container {
  width: 100vw;
  height: 100%;
  background: purple;
}

body {
  margin: 0px;
  
}

#innerDiv {
  height: 100vw;
  background-color: red;
  width: 100vw;

}

#innerDiv1 {
  height: 100%;
  background-color: blue;
  width: 100vw;
  
}

Html: Html:

<div id="container">
</div>

<div id="innerDiv">Div #1</div>
<div id="innerDiv1">Div #2</div>

You applied background purple color upto height of 100vh, if you want see purple fully you just add purple in body tag instead green or otherwise you want place div inside container.here is nothing problem.you just want to adjust your code您将背景紫色应用到 100vh 的高度,如果您想完全看到紫色,您只需在 body 标记中添加紫色而不是绿色,否则您希望将 div 放在容器中。这没有问题。您只想调整您的代码

 body { color: red; background: purple; margin: 0; padding: 0; } #container { width: 100vw; height: 100vh; z-index: 1; } p { font-size: 20pt; }
 <div id="container"></div> <p>ksksks</p>

The simplest solution is to make use of viewport height(vh) and viewport width(vw) units to set the height and width of the block.最简单的解决方案是利用viewport height(vh)viewport width(vw)单位来设置块的高度和宽度。 this block/div will fill up the entire space in the browser window.此块/div 将填满浏览器 window 中的整个空间。 Here is an example.这是一个例子。

div.container {
    height: 100vh;
    width: 100vw;
}

make sure you have the viewport meta tag in your <head> as given确保您在<head>中具有给定的视口元标记

<meta name="viewport" content="width=device-width, initial-scale=1.0">

This gives the browser instructions on how to control the page's dimensions and scaling.这为浏览器提供了有关如何控制页面尺寸和缩放的说明。 The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device). width=device-width部分设置页面的宽度以跟随设备的screen-width (这将因设备而异)。 The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser. initial-scale=1.0部分设置浏览器首次加载页面时的初始缩放级别。 Incase of a missing meta tag, the initial scale and zoom may misbehave.如果缺少元标记,初始缩放和缩放可能会出现异常。 Also put your <p> inside the container div.还将您的<p>放入容器 div 中。 Here is the working demo.这是工作演示。

My code:我的代码:

 HTML, body { margin: 0; padding: 0; } #container { width: 100vw; height: 100vh; background: purple; } p { height: 100%; width: 100%; background-color: rgba(255,255,255, .8); } <div id="container"></div>
 <div id="container"> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> </div>

It is just a workaround, I usually used it as mobile hamburger menu with full screen.这只是一种解决方法,我通常将其用作全屏移动汉堡菜单。 You have to be careful because it force to cover bfull page, it will hide all element if there is any other element already exists on the page.您必须小心,因为它会强制覆盖整个页面,如果页面上已经存在任何其他元素,它将隐藏所有元素。

.container {
    # it make the container always on the page regardless any element or window margin
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;

    height: 100vh;
    width: 100vw;
}

I have tried it with those codes and it seems like it's working, You can check them:我已经使用这些代码进行了尝试,它似乎可以正常工作,您可以检查它们:

 body { color: red; background: green; margin: 0; padding: 0; } #container { width: 100vw; height: 100vh; z-index: 1; background: purple; } p { font-size: 20pt; }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="main.css"> </head> <body> <div id="container"></div> <p>ksksks</p> </body> </html>

Nothing seems to be the problem.似乎没有什么问题。

 #container { width: 100vw; height: 100vh; background: purple; } body { margin: 0px; } #innerDiv { height: 100px; background-color: red; width: 100vw; } #innerDiv1 { height: 100px; background-color: blue; width: 100vw; }
 <div id="container"> </div> <div id="innerDiv">Div #1</div> <div id="innerDiv1">Div #2</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM