CSS制作一个单元网格,每个单元格填充其行的100%高度

时间:2011-12-29 19:09:54

标签: html css grid height

Example screenshot

如上例所示,我正在尝试创建一个盒子/单元格网格。我希望它看起来比屏幕截图更好,并且我希望每个单元格的底部向下延伸以与行中最高单元格的底部对齐。我知道有大约数百个帖子可以解决100%的高度问题,但是这些问题似乎都没有。

要求:

  • 无背景图片
  • 没有javascript
  • 必须使用以下Doctype:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">(Drupal 7)
  • 但是,我对标记和CSS非常灵活,例如,我可以添加额外的清除div甚至用表重新完成整个事情。

以下是我用于制作屏幕截图的代码:

HTML:

<div class="row">
  <div class="cell">Here is a grid of several cells. We want each cell to extend down to the bottom of the row.</div>
  <div class="cell">This cell it too short. </div>
</div>
<div class="row">
  <div class="cell">This cell should extend down to the bottom.</div>
  <div class="cell">We don't want to use background images or javascript. But the markup and CSS can be made however is best. Each row should contain cells of equal size.</div>
</div>

CSS:

.row {
  clear: both;
}

.cell {
  background: #CCC;
  border-radius: 10px;
  border: 2px solid #AAA;
  float: left;
  margin: 5px;
  padding: 5px;
  width: 200px;
}

2 个答案:

答案 0 :(得分:3)

您可以使用display:table-rowdisplay:table-cell完美实现此目标(除非您需要支持IE7及更低版本):

http://jsfiddle.net/ptriek/nFeCw/

.row {
    display:table-row;
}

.cell {
  background: #CCC;
  border-radius: 10px;
  border: 2px solid #AAA;
  display:table-cell;
  margin: 5px;
  padding: 5px;
  width: 200px;
}

答案 1 :(得分:2)

这就是为我做的......

enter image description here

HTML:

<table><tbody>
  <tr>
    <td>Here is a grid of several cells. We want each cell to extend down to the bottom of the row.</td>
    <td>This cell it too short. </td>
  </tr>
  <tr>
    <td>This cell should extend down to the bottom.</td>
    <td>We don't want to use background images or javascript. But the markup and CSS can be made however is best. Each row should contain cells of equal size.</td>
  </tr>
</tbody></table>

CSS:

table {
  border-spacing: 10px;
  margin: -10px;
  border-collapse: separate;
}

td {
  background: #CCC;
  vertical-align: top;
  border-radius: 10px;
  border: 2px solid #AAA;
  display: table-cell;
  padding: 5px;
  width: 200px;
}