13

我正在尝试使用 CSS 完成以下任务:

<table border="1" width="300px">
<tr>
    <td rowspan="2">This row should equal the height (no fixed-height allowed) of the 2 rows sitting to the right.</td>
    <td>Here is some sample text.  And some additional sample text.</td>
</tr>
<tr>
    <td>Here is some sample text.  And some additional sample text.</td>
</tr>
</table>

替代文字

我看到的用于实现此目的的示例使用固定高度或允许内容环绕左列。有没有一种优雅的方式来使用 CSS 来实现这一点?

4

3 回答 3

5

首先,你在做的事情对我来说就像一张桌子,所以你可能要考虑一下。然而,用 CSS 做这件事有点棘手(除非你用 CSS 做表格样式)。以下代码有效,但不会使框中的文本垂直居中:

<html><head><title>Test2</title></head><body>
<div style="width:300px; padding: 2px; border: 1px solid black;">
  <div style="float:right; width: 100px;">
    <div style="border: 1px solid black; margin-bottom: 2px;">
      Here is some sample text. And some additional sample text.
    </div>
    <div style="border: 1px solid black;">
      Here is some sample text. And some additional sample text.
    </div>
  </div>
  <div style="border: 1px solid black; margin-right: 102px;">
    <div>
      This column should equal the height (no fixed-height allowed) of the 2 rows sitting to the right.
    </div>
    <div style="clear: right; margin-bottom: -1px;"></div>
  </div>
</div></body></html>

CSS 中的表格单元格更容易:

<!DOCTYPE html>
<html><head><title>Test2</title></head><body>
<div style="display: table; width:300px; border: 1px solid black; border-spacing: 2px;">
  <div style="display: table-cell; border: 1px solid black; vertical-align: middle;">
    This column should equal the height (no fixed-height allowed) of the 2 rows sitting to the right.
  </div>
  <div style="display: table-cell; width: 100px;">
    <div style="border: 1px solid black; margin-bottom: 2px;">
      Here is some sample text. And some additional sample text.
    </div>
    <div style="border: 1px solid black;">
      Here is some sample text. And some additional sample text.
    </div>
  </div>
</div>
</body>
</html>
于 2010-04-09T22:50:36.917 回答
4

我需要一些非常相似的东西。不幸的是,所有这些解决方案都非常复杂,我带来了一些非常简单的东西(也许太简单了)——使用display: inline-block

HTML

<div>
    <span id="v">
        text in the left
    </span>
    <div id="x">
        <div>upper row</div>
        <div>lower row</div>
    </div>
</div>

CSS

#x {
    display: inline-block;
    vertical-align: middle;
}

小提琴

于 2013-05-03T10:31:13.950 回答
-1

这就是我使用的: http ://www.ejeliot.com/samples/equal-height-columns/example-7.html

我只是将第二列用作其他两个元素的包装器(语义较少)。这应该是最简单的方法。

于 2010-04-09T21:37:11.830 回答