简体   繁体   English

使用CSS将一行div设置为相同的高度

[英]Making a row of divs all be the same height using CSS

I have a row of divs that must all be the same height, but I have no way of knowing what that height might be ahead of time (the content comes from an external source). 我有一行div必须都是相同的高度,但我无法知道这个高度可能提前(内容来自外部源)。 I initially tried placing the divs in an enclosing div and floated them left. 我最初尝试将div放在一个封闭的div中并将它们向左浮动。 I then set their height to be "100%", but this had no perceptible effect. 然后我将它们的高度设置为“100%”,但这没有明显的效果。 By setting the height on the enclosing div to a fixed-height I could then get the floated divs to expand, but only up to the fixed height of the container. 通过将封闭div上的高度设置为固定高度,我可以将浮动的div扩展,但只能达到容器的固定高度。 When the content in one of the divs exceeded the fixed height, it spilled over; 当其中一个div中的内容超过固定高度时,溢出; the floated divs refused to expand. 浮动的divs拒绝扩张。

I Googled this floated-divs-of-the-same-height problem and apparently there's no way to do it using CSS. 我用Google搜索了这个浮动的div-of-the-height-problem问题,显然没有办法用CSS做到这一点。 So now I am trying to use a combination of relative and absolute positioning instead of floats. 所以现在我试图使用相对和绝对定位的组合而不是浮点数。 This is the CSS: 这是CSS:

<style type="text/css">
div.container {
  background: #ccc;
  position: relative;
  min-height: 10em;
}
div.a {
  background-color: #aaa;
  position: absolute;
  top: 0px;
  left: 0px;
  bottom: 0px;
  width: 40%;
}
div.b {
  background-color: #bbb;
  position: absolute;
  top: 0px;
  left: 41%;
  bottom: 0px;
  width: 40%;
}
</style>

This is a simplified version of the HTML: 这是HTML的简化版本:

   <div class="container">
    <div class="a">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit.</div>
    <div class="b">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit.</div>
   </div>

This works, unless you change the min-height to something like 5em (demonstranting what happens when the content exceeds the minimum height), and you can see that while the text doesn't get cutoff, the divs still refuse to expand. 这是有效的,除非你将min-height更改为类似于5em(展示内容超出最小高度时会发生什么),并且你可以看到,虽然文本没有被截断,但div仍然拒绝扩展。 Now I am at a lose. 现在我输了。 Is there any way to do this using CSS? 有没有办法用CSS做到这一点?

Here is one of those moments where you can get stuck between being idealistic or realistic. 这是一个你可以陷入理想主义或现实之间的时刻。 I understand that there is no semantic value to placing non-tabular data in a table strictly for formatting reasons but I don't want to see you bending over backwards to create a non-tabular solution to this problem simply for its own sake. 我理解,出于格式化的原因,将非表格数据放在表格中没有语义价值,但是我不希望看到你向后弯腰以便为这个问题创建一个非表格式的解决方案,仅仅是为了它自己。

I am the first to shoot down non-semantic designs, trust me, but sometimes you need to face the fact that CSS + semantic markup does not work for all design scenarios. 我是第一个击落非语义设计的人,相信我,但有时你需要面对CSS +语义标记不适用于所有设计场景的事实。 I don't know the accessibility needs of this site but I would recommend that you look to a more practical solution to this problem. 我不知道这个网站的可访问性需求,但我建议你寻找一个更实际的解决方案来解决这个问题。

Cheers to you for approaching this the right way and looking for the proper way to solve it! 欢呼你以正确的方式接近这个并寻找解决它的正确方法! Unfortunately this is one of the dark corners of CSS (along with vertical positioning within a block) that is just plain impossible to do without faux columns, javascript, or table cells. 不幸的是,这是CSS的黑暗角落之一(以及块内的垂直定位)​​,如果没有虚拟列,javascript或表格单元格,这是不可能的。

Whichever you choose, please don't adhere to a standard for its own sake. 无论您选择哪种,请不要为了自己的利益而遵守标准。

Making them exactly the same height can be a tricky thing, but if they just have to appear to be the same height, you may want to look at the faux columns technique. 使它们完全相同的高度可能是一个棘手的事情,但如果它们只是看起来是相同的高度,你可能想看看人造柱技术。

I have tried a few other methods, but this is the most reliable way I have found of getting the effect, apart from using tables of course. 我尝试了其他一些方法,但这是我发现获得效果的最可靠方法,当然除了使用表格。

You can use JavaScript, but of course it will break without JavaScript enabled. 您可以使用JavaScript,但当然它会在没有启用JavaScript的情况下中断。 Then there's tables, but this ruins the point of CSS. 然后是表格,但这破坏了CSS的重点。 Perhaps seanb's answer could work, place a background image that creates the illusion that all columns go to the bottom. 也许seanb的答案可以起作用,放置一个背景图像,产生所有列都到底的错觉。 This is known as the faux background technique . 这被称为人造背景技术

Or sit tight and wait for display: table-cell to be supported for all/most browsers. 或者坐下来等待display: table-cell所有/大多数浏览器都支持display: table-cell

OK, the table thing turned out to be a little trickier than I thought it would be. 好吧,桌子上的东西比我想象的要复杂一点。 It turns out the Javascript solution is actually the simplest (for my situation), since my app is an AJAX app and the framework uses Prototype.js. 事实证明,Javascript解决方案实际上是最简单的(对于我的情况),因为我的应用程序是一个AJAX应用程序,框架使用Prototype.js。 All it takes is a few lines of JS: 所需要的只是几行JS:

$$('div.container').each(function (element) {
 var longest = 0;

 element.descendants().each(function (child) {
  if (child.getHeight() > longest)
   longest = child.getHeight();
 });

 element.descendants().each(function (child) {
  child.style.height = longest + 'px';
 });
});

Thanks again for the help. 再次感谢您的帮助。

Thanks for the answers, guys. 伙计们,谢谢你的回答。 I don't think the background image will work because the widths of the columns can also vary depending on how many columns there are (the user can change it). 我不认为背景图像会起作用,因为列的宽度也可以根据有多少列(用户可以更改它)而变化。 I guess I'll use tables :( 我想我会用表:(

if your looking for a purely css option, and you can seperate the background of the block from the content then the answer is two have two bits of code for each block, one for the content, one for the background. 如果你正在寻找一个纯粹的css选项,并且你可以从内容中分离块的背景,那么答案就是两个,每个块有两位代码,一个用于内容,一个用于背景。 this is how it's done: 这就是它的完成方式:

<div id="wrapper">
  <div class="content" id='one>
  <div class="content" id="two>
  <div class="content" id="three>
  <div class="background" id="back-one">
  <div class="background" id="back-two">
  <div class="background" id="back-three">
</div>

Position the content divs using floats. 使用浮动定位内容div。 Then position the backgrounds using absolute positioning (and a z-index to put them behind) 然后使用绝对定位(和z-index将它们放在后面)定位背景

This works because the floats can set the height, and the absolutely positioned elements can have 100% height. 这是有效的,因为浮子可以设置高度,绝对定位的元素可以有100%的高度。 It's a little bit messy, but does the job. 这有点乱,但做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM