【发布时间】:2015-04-20 20:40:01
【问题描述】:
我有两个行数不同的浮动表(请参阅 JSFiddle)。
我希望这两个表格具有相同的高度,而无需将高度明确设置为例如 400 像素。我尝试按照this 问题的答案中的建议将它们放入高度为 100% 的 div 容器中,但这只会导致表格不再水平对齐。
有没有办法只使用 HTML 和 CSS 来实现这一点?
这是表格的 HTML 代码:
<table class="left">
<tr>
<th>Row1</th>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
</tr>
<tr>
<th>Row2</th>
<td>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</td>
</tr>
<tr>
<th>Row3</th>
<td>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </td>
</tr>
<tr>
<th>Row4</th>
<td>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</td>
</tr>
</table>
<table class="right">
<tr>
<th>1</th>
<td>Monday</td>
</tr>
<tr>
<th>2</th>
<td>Tuesday</td>
</tr>
<tr>
<th>3</th>
<td>Wednesday</td>
</tr>
<tr>
<th>4</th>
<td>Thursday</td>
</tr>
<tr>
<th>5</th>
<td>Friday</td>
</tr>
<tr>
<th>6</th>
<td>Saturday</td>
</tr>
<tr>
<th>7</th>
<td>Sunday</td>
</tr>
</table>
使用以下 CSS:
table {
border-collapse: collapse;
border-spacing: 0;
}
table td, th {
border: 1px solid darkgrey;
text-align: left;
padding: 0.4em;
}
table.left {
width: 70%;
height: 400px;
float: left;
}
table.right {
width: 25%;
height: 400px;
float: right;
}
【问题讨论】:
-
在页面加载时使用 jquery 计算第一个表的高度,然后将其分配给第二个表。
-
您能否解释一下“...表格不再水平对齐”,您希望行如何对齐?您希望所有行的高度相同吗?
-
我希望表格的顶部和底部水平对齐(这可以在将高度设置为 400px 时实现),不一定是行。使用 div 时,表格不再浮动,而是“堆叠”在彼此之上。
标签: html css css-tables