简体   繁体   English

Div占用屏幕的总空间

[英]Div occupying total space of the screen

Like other pages, the main div is always a way in which occupies the entire screen seen by the User, and when he scroll down he can see another div: example1 , example2 像其他页面一样,主div总是占据用户看到的整个屏幕的一种方式,当他向下滚动时,他可以看到另一个div: example1example2

No matter if you resize the screen, the main div will always occupy the total space seen by the User. 不管您是否调整屏幕大小,主div始终将占据用户看到的总空间。

To test It I try this code below: 为了测试它,我尝试下面的代码:

<div style="background:yellow;  position:absolute; top:0; bottom:0; left:0; right:0; overflow:hidden; z-index:-1; float:left;">
    This is my Section!
</div>

With him I can see a large yellow background with a text occupying the entire area of my browser. 和他在一起,我可以看到一个黄色的大背景,上面的文字占据了我浏览器的整个区域。 Assuming I would like to add another div below this, how could I do that? 假设我想在此下方添加另一个div,该怎么做? Is possible with css or I will need javascript? 可能与CSS或我将需要JavaScript?

You can do it without javascript with only pure CSS. 您可以仅使用纯CSS 而无需使用 javascript。

With vh units, you can specify a margin-top on the next container like this : 使用vh单位,您可以在下一个容器中指定一个margin-top ,如下所示:

#content { margin-top: 100vh;}

The advantage of this method is that it is fully responsive, no matter how you resize it (height or width). 这种方法的优点是,无论您如何调整大小(高度或宽度),它都可以完全响应。

See it here 在这里看到

If you check your div more correctly, this code: 如果您更正确地检查div ,则此代码:

top: 0; bottom: 0; right: 0; 

Is what causes your div to occupy the whole screen because there isn't any space between the div since you set all of them at 0. 是什么原因导致div占据整个屏幕,因为div之间没有任何空格,因为将它们全部设置为0。

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

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