我最初的帖子表述不当,因此我决定简化我的问题并再试一次。(任何不便敬请谅解!)
我有一个容器 DIV(固定高度)并且想在里面垂直放置一张桌子。该表具有固定的标题和可滚动的 tbody。我希望 tbody 自动调整到周围容器的高度(减去头)。
.tbody {height: auto;}
不幸的是什么也没做。你知道一些技巧(flexbox?)来实现我的目标吗?
请参阅附件/codepen 作为起点...
body {
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}
.container { /* wrapper element for table */
width: 20%;
height: 20%;
background-color: lightcoral;
border: 3px solid black;
}
.tableX {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
text-align: left;
border: 1px dotted black;
}
.tableX thead {
display: block;
color: white;
background-color: darkgray;
}
.tableX tbody {
display: block;
overflow-y: auto;
color: black;
height: 100px; /* ----> this is the problem, I don't want a fixed height... */
}
.tableX tr {
height: 1.5rem;
}
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>TableTerror</title>
<link rel="stylesheet" href="tableTerror.css">
</head>
<body>
<div class="container">
<table class="tableX">
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>first company</td>
<td>first name</td>
<td>first place</td>
</tr>
<tr>
<td>some company</td>
<td>some name</td>
<td>some place</td>
</tr>
<tr>
<td>some company</td>
<td>some name</td>
<td>some place</td>
</tr>
<tr>
<td>some company</td>
<td>some name</td>
<td>some place</td>
</tr>
<tr>
<td>last company</td>
<td>last name</td>
<td>last place</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
PS:我不在乎(atm)列宽,滚动条的样式等。