使用 div 获得两个等高列的最简单方法
我有一个内容栏和一个侧边栏。内容栏有一个白色的背景,侧边栏有一个轻微的渐变背景图像。内容列始终比侧边栏长,因此侧边栏图像不会延伸到底部。如何使用 div 始终使内容和侧边栏在垂直方向上具有相同的高度?
I have a content column and a sidebar. The content column has a white bg, the sidebar has a slight gradient bg image. The content column is always longer than the sidebar, so the sidebar image doesn't stretch to the bottom. How can I always make the content and sidebar columns the same height vertically using divs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然不是唯一的方法,但我最喜欢的是使用“假列”技巧:http: //www.alistapart.com/articles/fauxcolumns/
基本思想是将渐变背景放置在包裹内容和侧边栏的 div 中。该包装器 div 保证与最长的 DIV 一样大,因此您的渐变始终可见。
Although not the only way, to do it, my favorite is to use the "faux column" trick: http://www.alistapart.com/articles/fauxcolumns/
The basic idea is to place the gradient background in a div that wraps both the content and the sidebar. That wrapper div is guaranteed to be as big as the longest DIV, therefore your gradient is always visible.
position:absolute
通常被认为是不好的形式,但可以使用以下代码实现等高列:HTML:
CSS:
Combining
top: 0
和Bottom: 0
导致元素拉伸到其极限(只要没有声明高度)。请参阅 jsFiddle 进行演示。position: absolute
is often considered bad form, but it is possible to achieve equal-height columns using the following code:HTML:
CSS:
Combining
top: 0
andbottom: 0
causes the element to stretch to its limits (as long as no height is declared). See the jsFiddle for a demonstration.有许多不同的方法可以实现这一点。我发现,正确的解决方案很大程度上取决于您的 HTML 架构。
话虽这么说,如果您在使用 HTML/CSS 时遇到困难,我出于绝望而使用的解决方法是使用 JS(在本例中为 jQuery)。我并不是仅仅因为这个原因就建议您将 jQuery 包含在您的项目中。我只是提供一个我以前用过的替代方案。
http://jsfiddle.net/zZpuU/
There are many different ways to accomplish this. I've found, the correct solution depends greatly on your HTML Architecture.
That being said, if you're having a hard time with HTML/CSS, a work-around that I have used out of desperation is to use JS (in this example, jQuery). I am not suggesting you include jQuery in your poject merely for this reason. I am simply offering an alternative that I have used before.
http://jsfiddle.net/zZpuU/