HTML 正文高度 - 固定吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4889836/
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 06:30:52  来源:igfitidea点击:

HTML body height - fixed?

htmlcss

提问by tom

I'm trying to make the main body of my site to have a fixed height (I think!).

我试图使我的网站的主体具有固定的高度(我认为!)。

Anyway, the site body is just white, with a border of size 1. Basically, the size of the body is determined by what's in it so, for example, it will resize automatically as more things are added.

无论如何,站点主体是白色的,边框为1。基本上,主体的大小由其中的内容决定,例如,随着添加更多内容,它会自动调整大小。

What I want is vertical scroll bars so the body doesn't extend forever (is that right?). Should I have a fixed body size?

我想要的是垂直滚动条,所以主体不会永远延伸(是吗?)。我应该有一个固定的体型吗?

回答by goggin13

If you want vertical scroll bars you can use this in your CSS;

如果你想要垂直滚动条,你可以在你的 CSS 中使用它;

body {
   height: 900px;
   overflow-y: scroll;
   overflow-x: hidden; /* hides the horizontal scroll bar */
}

What are you trying to accomplish? You sound unsure if you even want the scroll bars; is there a reason you want to show the scroll bars instead of just having the browser handle the scrolling when the content gets larger than the window?

你想达到什么目的?你听起来不确定你是否想要滚动条;当内容大于窗口时,您是否有理由要显示滚动条而不是让浏览器处理滚动?

回答by KyleWpppd

Yes. You need a fixed height

是的。你需要一个固定的高度

body{
height: your-height;
overflow:auto;
}

will generate scroll bars only when you overflow the area without growing it vertically.

仅当您溢出区域而不垂直增长时才会生成滚动条。

回答by Mohamad

So, in your body create a layer:

所以,在你的身体中创建一个层:

<div id="mainbar">
</div>

And using CSS you can set the height:

使用 CSS 你可以设置高度:

div#mainbar {
    min-height:100px;
    overflow:auto;
}

The min-heightguarantees the initial height that you need. Once it goes over that, it you will automatically have scrollbars. If you would rather the page itself scroll and the body lengthen, just take out the overflowline from the CSS.

min-height保证初始高度,你所需要的。一旦它超过那个,它就会自动有滚动条。如果您希望页面本身滚动而正文变长,只需overflow从 CSS 中取出该行即可。

回答by JakeParis

If you want the vertical scroll bars to an inner div on your site (like so you can have a footer visible at all times), simple specify the height of the div:

如果您希望垂直滚动条位于您网站上的内部 div(就像您可以始终看到页脚一样),只需指定 div 的高度:

#inner { max-height: 300px;
    }

I think the default for the overflow is to scroll, but if your content is cutting cut off with no scrollbars, you could also set

我认为溢出的默认设置是滚动,但是如果您的内容在没有滚动条的情况下被切断,您也可以设置

overflow: auto;