使用CSS使一行div的高度相同 [英] Making a row of divs all be the same height using CSS

查看:548
本文介绍了使用CSS使一行div的高度相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一排div,它们必须都具有相同的高度,但是我无法提前知道该高度是多少(内容来自外部来源)。我最初尝试将div放在封闭的div中,然后将它们浮动到左边。然后,我将它们的高度设置为 100%,但这没有明显的效果。通过将封闭的div的高度设置为固定高度,我可以使浮动的div扩展,但只能达到容器的固定高度。当其中一个div的内容超出固定高度时,它溢出了;



我用Google搜索了相同高度的float-divs问题,显然没有办法使用CSS来实现。因此,现在我尝试使用相对和绝对定位的组合,而不是浮点数。这是CSS:

 < style type = text / css> 
div.container {
背景:#ccc;
职位:相对;
最低高度:10em;
}
div.a {
background-color:#aaa;
头寸:绝对;
top:0像素;
左:0px;
bottom:0px;
宽度:40%;
}
div.b {
background-color:#bbb;
头寸:绝对;
top:0像素;还剩
:41%;
bottom:0px;
宽度:40%;
}
< / style>

这是HTML的简化版本:

 < div class = container> 
< div class = a&L; Lorem ipsum dolor坐着,安全管教。 Integer pretium dui坐在。整数坐amet直径。小孔菜豆。
< div class = b> Lorem ipsum dolor坐着,安全管教。 Integer pretium dui坐在。整数坐amet直径。小菜豆。 Lorem ipsum dolor坐下来,管教精通精英。 Integer pretium dui坐在。整数坐amet直径。小孔菜豆。
< / div>

这是可行的,除非您将最小高度更改为5em之类(验证内容时发生的情况)超过最小高度),您会看到虽然文本没有截断,但div仍然拒绝扩展。现在我很茫然。有没有办法使用CSS做到这一点?

解决方案

在这里,您可能会陷于理想化或僵化之间。现实的。我了解严格地出于格式化原因,将非表格数据放置在表中没有语义上的价值,但我不想看到您为了自己的问题而弯腰为该问题创建非表格解决方案。 / p>

我是第一个拒绝非语义设计的人,请相信我,但是有时您需要面对这样一个事实,即CSS +语义标记不适用于所有设计方案。我不知道该网站的可访问性需求,但我建议您寻求一个更实际的解决方案。



为您提供帮助,为您加油打气方式并寻找解决问题的正确方法!不幸的是,这是CSS的暗角之一(以及在块中的垂直定位)​​,如果没有人造列,javascript或表单元格,这简直是不可能的。



无论您选择哪种方式,请不要为了自己而遵守标准。


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). I initially tried placing the divs in an enclosing div and floated them left. I then set their height to be "100%", but this had no perceptible effect. 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. When the content in one of the divs exceeded the fixed height, it spilled over; the floated divs refused to expand.

I Googled this floated-divs-of-the-same-height problem and apparently there's no way to do it using CSS. So now I am trying to use a combination of relative and absolute positioning instead of floats. This is the 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:

   <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. Now I am at a lose. Is there any way to do this using 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. 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.

Whichever you choose, please don't adhere to a standard for its own sake.

这篇关于使用CSS使一行div的高度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆