I was looking for a way to add a vertical scrollbar to my table, and I ended up wrapping the entire table in a div. The result was that the table head was fixed in position. Is there a simpler method to include a scrollbar in a table without affecting the width of the table while keeping it centered on the page?
thead, tr, th, td, tbody{
border: 1px solid;
text-align: center;
padding: 3px;
}
th{
background-color:#99ccff;
height: 40px;
font-size: 20px;
}
tr{
width: 500%;
height: 20px;
font-size: 17px;
}
tr:nth-child(even) {
background-color: #CCFFFF;
}
tr:nth-child(odd) {
background-color: #fae8d1;
}
thead{
position: fixed;
width: 1200px;
}
.tbldiv{
width: 1200px;
height: 600px;
border: 2px solid;
overflow: auto;
}
<div class="tbldiv">
<table class="scroll">
<thead>
<tr>
<th class="col-md-2">Name</th>
<th class="col-md-2">Birthday</th>
<th class="col-md-2">Gender</th>
<th class="col-md-2">Marital</th>
<th class="col-md-2">Address</th>
<th class="col-md-2">Telephone</th>
<th class="col-md-2">Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
</tbody>
</table>
</div>