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

发布于 2024-12-23 09:26:40 字数 1219 浏览 1 评论 0原文

示例屏幕截图

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

要求:

  • 无背景图像
  • 无 javascript
  • 必须使用以下 Doctype: (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;
}

Example screenshot

As shown in the example above, I'm trying to create a grid of boxes/cells. I want it to look nicer than the screenshot, and I want the bottom of each cell extend down to align with the bottom of the tallest cell in the row. I know there are about a millions posts to solve 100% height problems but none of them seem to work for this case.

Requirements:

  • No background images
  • No javascript
  • Must work with the following Doctype: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> (Drupal 7)
  • But, I'm very flexible with the markup and CSS, for example, I'm fine with adding extra clear divs or even re-doing the whole thing with tables.

Here's the code I used to make the screenshot above:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-12-30 09:26:40

您可以使用 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;
}

You could perfectly achieve this with display:table-row and display:table-cell (unless you need to support IE7 and lower):

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;
}
扬花落满肩 2024-12-30 09:26:40

这就是为我做的...

在此处输入图像描述

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;
}

Here's what did it for me...

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文