I want a table with the tag thead
fixed at the top and tbody
with a vertical scroll, I found some solutions that only use CSS, but use the display
property, removing the default behavior from the table and making the same as block
, making it impossible to use auto-adjustable fields, creating misalignment with the title and data.
dbgrid
{
text-align: left;
width: 100%;
}
.dbgrid table
{
width: 100%;
text-align: left;
border-collapse: collapse;
vertical-align: top;
white-space: nowrap;
text-overflow: ellipsis;
}
.dbgrid thead
{
background: #063b53;
color: #fff;
width:100%;
}
.dbgrid tr
{
width:100%;
box-sizing: border-box;
padding: 3px 10px;
text-align: left;
}
.dbgrid thead tr
{
color:#fff;
background:#000;
}
.dbgrid tbody tr
{
color:#000;
background:#FFF;
}
/** Testar barra de scroll com height:40px; **/
.dbgrid tbody
{
width:100%;
text-align: left;
overflow-y: scroll;
overflow-x: hidden;
height:40px;
/** ao utilizar display:block funciona, porém perde algumas propriedades da tabela, como alinhar o título da coluna.
display:block;
**/
}
<div class="dbgrid">
<table>
<thead>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Sobrenome</th>
<th>Endereço</th>
</tr>
</thead>
<tbody>
<tr>
<td>01</td>
<td>João</td>
<td>Silva</td>
<td>Avenida Genérica N 12345</td>
</tr>
<tr>
<td>02</td>
<td>Pedro</td>
<td>Souza</td>
<td>Rua Comum N 65478 - Bairro Qualquer</td>
</tr>
<tr>
<td>03</td>
<td>João</td>
<td>Paulo</td>
<td>-</td>
</tr>
<tr>
<td>04</td>
<td>Maria</td>
<td>Carvalho</td>
<td>Avenida Qualquer</td>
</tr>
</tbody>
</table>
</div>
This is the table I have, remove the comment from display:block
and then the scroll will work, however the fields will misalign.
Is there a solution? I accept solutions that use pure CSS or Javascript, or some combination of both.
Thank you.