I have searched a lot of times to get a right solution to make the table header (thead) as fixed when we scroll down the body (tbody) section. So many solutions are right there, but nothing help me.
Some of tried links:
- How to make Scrollable Table with fixed headers using CSS
- https://jsfiddle.net/dPixie/byB9d/3/
- http://maxdesign.com.au/jobs/sample-fixed-thead/index.htm
Most of the links are using a fixed width for each tr td
but I need it without specify any width to elements, because the table is generating dynamically. Title & contents are might be long or short, so the width is depending upon the content.
Have any other solution to align th
and td
properly?
.fixed_headers {
width: 100%;
table-layout: fixed;
border-collapse: collapse;
}
.fixed_headers thead tr {
display: block;
position: relative;
}
.fixed_headers tbody {
display: block;
overflow: auto;
width: 100%;
max-height: 200px;
}
<table class="fixed_headers" border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Lorem Ipsums</th>
<th>ipsum dolor sit amet</th>
<th>consectetur adipiscing</th>
<th>do eiusmod tempor incididunt</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>quis nostrum exercitationem</td>
</tr>
<tr>
<td>quis nostrum exercitationem</td>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
</tr>
<tr>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>quis nostrum exercitationem</td>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
</tr>
<tr>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
<td>quis nostrum exercitationem</td>
</tr>
<tr>
<td>accusantium doloremque laudantium</td>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>quis nostrum exercitationem</td>
</tr>
<tr>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>quis nostrum exercitationem</td>
</tr>
<tr>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
<td>quis nostrum exercitationem</td>
</tr>
<tr>
<td>accusantium doloremque laudantium</td>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>quis nostrum exercitationem</td>
</tr>
<tr>
<td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</td>
<td>accusantium doloremque laudantium</td>
<td>consequuntur magni dolores eos qui ratione voluptatem</td>
<td>quis nostrum exercitationem</td>
</tr>
</tbody>
</table>