My initial post was poorly formulated, therefor I decided to simplify my question and try it again. (Sorry for any inconvenience!)
I have a container DIV (fixed height) and want to vertically fit a table inside. The table has fixed headers and a scrollable tbody. I want the tbody to automatically adjust to the height of the surrounding container (minus the thead).
.tbody {height: auto;}
Does nothing unfortunately. Do you know of some trickery (flexbox?) to achieve my goal?
Please see the attached files/codepen as a starting point...
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: I don't care (atm) for column-width, the style of the scrollbar etc.
I'm trying to replicate your image. Is this the layout that you need?