I want a table where header is sticky and body should be scrollable and the whole table should have rounded corners.
This is my code.
<div class="col-lg-4 table-container">
<table>
<thead>
<tr>
<th class="table_header">DEPARTMENTS</th>
</tr>
</thead>
<tbody class="scrollable-body">
<tr *ngFor="let department of departments">
<td>{{ department }}</td>
</tr>
</tbody>
</table>
</div>
And CSS
.table-container {
max-height: 200px; /* Set a maximum height for the container */
overflow-y: auto; /* Enable vertical scrolling */
}
.table_header {
padding: 5%;
color: blue;
}
table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
thead {
position: sticky;
top: 0;
background-color: white;
color: #015468;
}
tbody.scrollable-body {
display: block;
max-height: inherit;
overflow-y: auto;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
}
The corners are not rounded and the rows in tbody are not coming till the scroll bar, there is so much gap between tbody and scroll.
What changes I need to make to my html and css , so that I can get the table I need?
Please help! Thanks in advance!!!
2
Answers
Try this
You may also try doing it specifically for tbody, but this way make it a bit more trickier to work with
tr
andtd
. In this case you setdisplay: block;
to make tbody scrollable, but it breaks the table structure which is not the best thing, but if you have a simple table, it should work.jsfiddle sandbox link