Struggling to create a scrolling table with CSS? When using overflow-y : auto
, the <td>
tag becomes overloaded in the <tr>
element. Take a look at the example code below to see the HTML
and CSS
in action:
HTML CODE :
<table id="check" class="table table-fixed">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Jax</td>
<td>Male</td>
</tr>
<!-- More table rows here -->
</tbody>
CSS CODE :
#check {
width: 70%;
margin-top : 5%;
margin-left: 45%;
border-collapse: collapse;
width: 100%;
}
#check td, #check th {
border: 1px solid #ddd;
padding: 8px;
}
#check tr:nth-child(even){background-color: #f2f2f2;}
#check tr:hover {background-color: #ddd;}
#check th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
.table-fixed tbody {
height: 250px;
overflow-y: auto;
margin-top: 1px;
table-layout: fixed;
display: block;
overflow: auto;
}
.table-fixed thead tr {
display: block;
}
You can experiment with this code in an online code editor like here.