2
I want a table with the tag thead
fixed at the top and tbody
with a scroll vertically, I found some solutions that use only CSS, but use the property display
, removing the default behavior from the table and making it the same in block
, making it impossible to use self-adjusting fields, creating a misalignment with 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, but the fields will misalign.
Is there a solution? I accept solutions that use pure CSS or Javascript, or some combination of both.
Grateful.
I had to do something similar, only the scroll was horizontal, then I did it with two tables and Divs that enclosed them to determine the scroll.
– fernandoandrade
I also have the possibility to use a div to encompass everything, but the problem is that the scroll is vertical, and I would like the title of the columns to be maintained, in this case if you used the same solution as yours, the title of the columns would scroll next to the scroll.
– Renan Cavalieri
You make a div for the title and another for tbody, then scroll you apply only in tbody div, like that example, there at the end of the page on the tab cards, just do it backwards
– fernandoandrade
It is not possible to use this logic vertically, as you cannot use Ivds in the table that are not within the td or th element, it would act in the same way as setting tbody to block. As I pointed out earlier, the solution is perfect for horizontal use, but vertical does not work.
– Renan Cavalieri
But it is to put div inside the table no, it would be one to simulate thead and another for tbody.
– fernandoandrade