将 HTML 页面分成水平部分,没有垂直滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19239331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:12:51  来源:igfitidea点击:

Dividing HTML page into horizontal sections, without vertical scrollbars

htmlcsslayout

提问by Mehdi Emrani

I'm trying to create something like this:

我正在尝试创建这样的东西:

http://jsfiddle.net/S6FUQ/

http://jsfiddle.net/S6FUQ/

HTML is:

HTML是:

<div id="container">
    <header></header>
    <main>
        <section class="half"></section>
        <section class="half"></section>
    </main>
</div>

And CSS is:

而 CSS 是:

* {
    margin: 0; padding: 0;
}
html, body, #container {
    height: 100%;
}
header {
    height: 50px;
    background: gray;
}
main {
    height: 100%;
    background: green;
}
.half {
    height: 50%;
}
.half:first-child {
    background: blue;
}
.half:last-child {
    background: yellow;
}

In it, I have a thin ribbon at the top, and I want to divide the rest of the screen into two equal sections, but I don't want vertical scrollbar to appear.

其中,我在顶部有一条细丝带,我想将屏幕的其余部分分成两个相等的部分,但我不希望出现垂直滚动条。

I tried margin-bottom: 50px;for main, but it didn't work. What should I do?

我试过margin-bottom: 50px;main,但没有奏效。我该怎么办?

回答by Hiral

Height of "main" should be 100% - 50px. Here is the fiddle.

“main”的高度应该是 100% - 50px。这是小提琴

main{height: calc(100% - 50px);}

回答by Oriol

To make it work on old browsers, you could use absolute positioning.

要使其在旧浏览器上工作,您可以使用绝对定位。

Demo

演示

#container {
    position: relative;
}
main {
    position: absolute;
    width: 100%;
    top: 50px;
    bottom: 0;
    background: green;
}

回答by Thanos

You are already using % to set height... Why don't you use it again to solve your problem?

您已经在使用 % 来设置高度...为什么不再次使用它来解决您的问题?

header {
    height: 10%;
    background: gray;
    max-height:50px; //this will ensure your header will never go bigger than 50px
}
main {
    height: 90%;
    background: green;
}

PS: The only time your header is going to be smaller than 50px is when the browser is smaller than 500px (which will be only in some landscape mobile devices)

PS:标题小于 50px 的唯一时间是浏览器小于 500px(仅在某些横向移动设备中)

EXAMPLE

例子