div 具有基于浏览器窗口高度的动态最小高度
我有三个 div
元素:一个作为页眉,一个作为页脚,以及一个中心内容 div
。
中心的 div
需要随内容自动扩展,但我想要一个 min-height
,以便底部的 div
始终至少到达窗口底部,但在较长的页面上并没有固定在那里。
例如:
<div id="a" style="height: 200px;">
<p>This div should always remain at the top of the page content and should scroll with it.</p>
</div>
<div id="b">
<p>This is the div in question. On longer pages, this div needs to behave normally (i.e. expand to fit the content and scroll with the entire page). On shorter pages, this div needs to expand beyond its content to a height such that div c will reach the bottom of the viewport, regardless of monitor resolution or window size.
</div>
<div id="c" style="height: 100px;">
<p>This div needs to remain at the bottom of the page's content, and scroll with it on longer pages, but on shorter pages, needs to reach the bottom of the browser window, regardless of monitor resolution or window size.</p>
</div>
I have three div
elements: one as a header, one as a footer, and a center content div
.
the div
in the center needs to expand automatically with content, but I would like a min-height
such that the bottom div
always at least reaches the bottom of the window, but is not fixed there on longer pages.
For example:
<div id="a" style="height: 200px;">
<p>This div should always remain at the top of the page content and should scroll with it.</p>
</div>
<div id="b">
<p>This is the div in question. On longer pages, this div needs to behave normally (i.e. expand to fit the content and scroll with the entire page). On shorter pages, this div needs to expand beyond its content to a height such that div c will reach the bottom of the viewport, regardless of monitor resolution or window size.
</div>
<div id="c" style="height: 100px;">
<p>This div needs to remain at the bottom of the page's content, and scroll with it on longer pages, but on shorter pages, needs to reach the bottom of the browser window, regardless of monitor resolution or window size.</p>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只需查找我的 jsfiddle 上的解决方案,它基于 csslayout
Just look for my solution on jsfiddle, it is based on csslayout
我发现这是由 ryanfait.com 提供的。实际上非常简单。
为了在内容短于窗口高度时将页脚浮动到页面底部,或者在内容长于窗口高度时将页脚浮动到内容底部,请使用以下代码:
基本 HTML 结构:
注意:除非绝对定位,否则不应将任何内容放置在“#content”和“#footer”div 之外。
注意:“#push”div 内不应放置任何内容,因为它将被隐藏。
CSS:
要使页眉或页脚跨越页面的宽度,您必须绝对定位页眉。
注意:如果添加页面宽度标头,我发现有必要向 #content 添加额外的包装 div。外部 div 控制水平间距,内部 div 控制垂直间距。我被要求这样做是因为我发现 'min-height:' 只适用于元素的主体并为高度添加填充。
*编辑:缺少分号
I found this courtesy of ryanfait.com. It's actually remarkably simple.
In order to float a footer to the bottom of the page when content is shorter than window-height, or at the bottom of the content when it is longer than window-height, utilize the following code:
Basic HTML structure:
Note: Nothing should be placed outside the '#content' and '#footer' divs unless it is absolutely positioned.
Note: Nothing should be placed inside the '#push' div as it will be hidden.
And the CSS:
To make headers or footers span the width of a page, you must absolutely position the header.
Note: If you add a page-width header, I found it necessary to add an extra wrapper div to #content. The outer div controls horizontal spacing while the inner div controls vertical spacing. I was required to do this because I found that 'min-height:' works only on the body of an element and adds padding to the height.
*Edit: missing semicolon
如果
#top
和#bottom
具有固定高度,您可以使用:update
如果您希望
#central
向下拉伸,您可以:calc()
;min-height
。使用
calc()
:使用 jQuery,它可能类似于:
If
#top
and#bottom
have fixed heights, you can use:update
If you want
#central
to stretch down, you could:calc()
;min-height
.With
calc()
:With jQuery it could be something like:
根据浏览器窗口获取动态高度。使用 vh 而不是 %
例如:将以下 height: 100vh; 传递给特定 div
to get dynamic height based on browser window. Use vh instead of %
e.g: pass following height: 100vh; to the specific div
正如其他地方提到的,CSS 函数 calc() 在这里可以很好地工作。现在大部分都支持了。你可以这样使用:
As mentioned elsewhere, the CSS function calc() can work nicely here. It is now mostly supported. You could use like:
无需 hack 或 js。只需将以下规则应用于您的根元素:
它会自动从两者中选择较大的一个作为其高度,这意味着如果内容比浏览器长,则为内容的高度,否则为内容的高度浏览器。这是标准的CSS。
No hack or js needed. Just apply the following rule to your root element:
It will automatically choose the bigger one from the two as its height, which means if the content is longer than the browser, it will be the height of the content, otherwise, the height of the browser. This is standard css.
您可能必须编写一些 JavaScript,因为无法估计页面所有用户的身高。
You propably have to write some JavaScript, because there is no way to estimate the height of all the users of the page.
这很难做到。
有一个
min-height:
css 样式,但它并不适用于所有浏览器。你可以使用它,但最大的问题是你需要将其设置为 90% 或类似的数字(百分比),但顶部和底部 div 使用固定像素大小,你将无法协调他们。我知道
a
和c
具有固定的高度,但我宁愿测量它们,以防以后它们发生变化。另外,我正在测量
b
的高度(毕竟我不想做得更小),但如果其中有未加载的图像,高度可能会改变,所以要小心对于这样的事情。这样做可能更安全:
只需将一个具有正确高度的元素添加到该 div 中 - 即使对于没有它的浏览器,它也可以有效地充当最小高度。 (您可能想要将元素添加到标记中,然后只需通过 javascript 控制它的高度,而不是也以这种方式添加它,这样您就可以在设计布局时考虑到它。)
It's hard to do this.
There is a
min-height:
css style, but it doesn't work in all browsers. You can use it, but the biggest problem is that you will need to set it to something like 90% or numbers like that (percents), but the top and bottom divs use fixed pixel sizes, and you won't be able to reconcile them.I know
a
andc
have fixed heights, but I rather measure them in case they change later.Also, I am measuring the height of
b
(I don't want to make is smaller after all), but if there is an image in there that did not load the height can change, so watch out for things like that.It may be safer to do:
Which simply adds an element into that div with the correct height - that effectively acts as min-height even for browsers that don't have it. (You may want to add the element into your markup, and then just control the height of it via javascript instead of also adding it that way, that way you can take it into account when designing the layout.)