I’m trying to get a scroll in the tbody (required to run a react package).
The problem is that to get the scrollable tbody, the only way I found is to use display: block
on tbody and that maps my whole tbody to the first column of the thead.
Is there a way to have a scrollable tbody without having this problem?
Codepen if needed.
table {
width: 100%
}
th {
background-color: green;
}
td {
background-color: red;
}
table {
width: 100%;
table-layout: fixed;
}
tbody {
display: block;
height: 300px;
width: 100%;
overflow-y: auto;
}
tr,
td {
height: 60px;
}
<table>
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
</tr>
</thead>
<tbody>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>Row1Col5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
<tr>
<td>RowCol1</td>
<td>RowCol2</td>
<td>RowCol3</td>
<td>RowCol4</td>
<td>RowCol5</td>
</tr>
</tbody>
</table>
2
Answers
I made some revision to the code, hope it works.
You can set the display of the
thead
asblock
as well and then give a width to eachth
andtd
to cover the total width as per your need.