In an attempt to create a fixed table header with a vertical scroll bar and maintain the size of the header, table data, and overall table width, I wrote some code. I included the table-responsive
class to automatically adjust the width for both the th
and td
.
My question is how can I achieve a vertical scroll with a fixed th
while keeping the table data size consistent?
thead th {
height: 30px;
}
tbody {
overflow-y: auto;
height: 190px;
}
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<div class="container">
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th>Sl No</th>
<th>Full Name</th>
<th>Email</th>
<th>Designation</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Unkown User</td>
<td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e6fdf8fdfce4fdd3f4fef2faffbdf0fcfebdf0fcfe">[email protected]</a></td>
<td>Unkown User</td>
<td>Unkown User</td>
</tr>
<tr>
<td>1</td>
<td>Unkown User Unkown User </td>
<td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097c676267667e67496e64686065276a6664276a6664">[email protected]</a></td>
<td>Unkown User</td>
<td>Unkown User</td>
</tr>
<tr>
<td>1</td>
<td>Unkown User</td>
<td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="196c777277766e77597e74787075377a7674377a7674">[email protected]</a></td>
<td>Unkown User</td>
<td>Unkown User</td>
</tr>
<tr>
<td>1</td>
<td>Unkown User</td>
<td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f7ece9ecedf5ecc2e5efe3ebeeace1edeface1edef">[email protected]</a></td>
<td>Unkown User</td>
<td>Unkown User</td>
</tr>
<tr>
<td>1</td>
<td>Unkown User</td>
<td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b6ada8adacb4ad83a4aea2aaafeda0acaeeda0acae">[email protected]</a></td>
<td>Unkown User</td>
<td>Unkown User</td>
</tr>
<tr>
<td>1</td>
<td>Unkown User</td>
<td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72071c191c1d051c32151f131b1e5c111d1f5c111d1f">[email protected]</a></td>
<td>Unkown User</td>
<td>Unkown User</td>
</tr>
</tbody>
</table>
</div>