HTML / CSS:使两个浮动div的高度相同

编程入门 行业动态 更新时间:2024-10-21 19:49:06
本文介绍了HTML / CSS:使两个浮动div的高度相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个特殊的问题,我目前解决使用表,见下文。基本上,我想有两个div占据100%的可用宽度,但只占据所需的垂直空间(这不是真的从图片显而易见)。

I have a little peculiar problem that I currently solve using a table, see below. Basically, I want to have two divs take up 100% of the available width, but only take up as much vertical space as needed (which isn't really that obvious from the picture). The two should at all times have the exact same height with a little line between them, as shown.

这是很容易做的使用 table 我现在在做。但是,我不太热衷于解决方案,因为在语义上这不是一个表。

This is all very easy to do using table, which I'm currently doing. However, I'm not too keen on the solution, as semantically this is not actually a table.

推荐答案

CSS中的高度列,通过应用大量的底部填充,相同量的底部负边距,并且使用具有溢出隐藏的div来围绕列。垂直居中的文本有点棘手,但这应该帮助你的路上。

You can get equal height columns in CSS by applying bottom padding of a large amount, bottom negative margin of the same amount and surrounding the columns with a div that has overflow hidden. Vertically centering the text is a little trickier but this should help you on the way.

#container { overflow: hidden; width: 100%; } #left-col { float: left; width: 50%; background-color: orange; padding-bottom: 500em; margin-bottom: -500em; } #right-col { float: left; width: 50%; margin-right: -1px; /* Thank you IE */ border-left: 1px solid black; background-color: red; padding-bottom: 500em; margin-bottom: -500em; }

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="www.w3/1999/xhtml" xml:lang="en"> <head></head> <body> <div id="container"> <div id="left-col"> <p>Test content</p> <p>longer</p> </div> <div id="right-col"> <p>Test content</p> </div> </div> </body>

值得一提的是,以前的回答是由streetpc有无效的html,doctype是XHTML,并且在属性周围有单引号。还值得注意的是,为了清除容器的内部浮动,你不需要一个额外的元素 clear 。如果你使用overflow隐藏,这将清除所有非IE浏览器中的浮动,然后只是添加一些东西给hasLayout,如宽度或缩放:1将导致IE清除其内部浮动。

I think it worth mentioning that the previous answer by streetpc has invalid html, the doctype is XHTML and there are single quotes around the attributes. Also worth noting is that you dont need an extra element with clear on in order to clear the internal floats of the container. If you use overflow hidden this clears the floats in all non-IE browsers and then just adding something to give hasLayout such as width or zoom:1 will cause IE to clear its internal floats.

我已经在所有现代浏览器FF3 + Opera9 + Chrome Safari 3+和IE6 / 7/8测试了这一点。它可能看起来像一个丑陋的伎俩,但它的工作原理很好,我在生产中使用它很多。

I have tested this in all modern browsers FF3+ Opera9+ Chrome Safari 3+ and IE6/7/8. It may seem like an ugly trick but it works well and I use it in production a lot.

我希望这有助于。

更多推荐

HTML / CSS:使两个浮动div的高度相同

本文发布于:2023-10-08 01:42:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高度   两个   HTML   CSS   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!