我正在尝试使用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实现这一目标?
答案 0 :(得分: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>
答案 1 :(得分: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;
}
答案 2 :(得分:0)
这是我使用的: http://www.ejeliot.com/samples/equal-height-columns/example-7.html
我只是使用第二列作为其他两个元素的包装(较少语义)。这应该是最简单的方法。