Is it possible to add scroll-only overflow behavior in a table's tbody?


You can add overflow behavior with scroll only in tbody and still do not have to set fixed sizes in pixels without having to separate the header of the columns with the body, as seen in the plugins that provide grids ?

css for overflow:

table.grid tbody {
    overflow-y: scroll;
    overflow-x: auto;


<table class='grid'>
    <thead> ... <thead>


    <tfoot> ... </tfoot>

The intention is that even a table having records that go beyond the boundary of the area in which it is allocated, that it does not overlap that limit, but that the records are navigable through the scroll bar, as in systems grids desktop, and who knows how to try to adapt it to be responsible.


Following is an example of how to just add css from overflow to tbody does not work:

As you can see in the image, the table is inserted in a div > Fixed 200px height . But table continues to exceed this limit.

asked by anonymous 12.09.2014 / 16:06

2 answers


It is possible, but the solution is somewhat convoluted because of the behavior of elements related to the <TABLE> element.

By default, tbody has the display:row-group property. This allows browsers to maintain alignment between cells in the same column in different parts of the table ( thead , tbody and tfoot ).

To allow overflow , you will need to change the display property of elements thead , tbody and tfoot to block . However, this creates a problem: you lose alignment between the header, body, and footer cells.

As workaround , you will need to coordinate the size of cells via JavaScript or JQuery.

There is a response in the original Stack Overflow that exactly covers this behavior. Here is the translation of the important points:


Browsers display the thead and tbody elements as row-group (    table-header-group and table-row-group ) by default.


Since we changed this, tr elements do not fill the entire   space of your container.


thead, tbody { display: block; }

tbody {
    height: 100px;       
    overflow-y: auto;    /* Exibe a barra de rolamento vertical     */
    overflow-x: hidden;  /* Esconde a barra de rolamento horizontal */


// Mude o seletor 'table' de acordo com sua necessidade.
var $table = $('table'),
    $bodyCells = $table.find('tbody tr:first').children(),

// Obtém o array de largura de colunas dentro de tbody
colWidth = $ {
    return $(this).width();

// Ajusta o tamanho das colunas no cabeçalho
$table.find('thead tr').children().each(function(i, v) {

A Fiddler with example of this process (including footer) can be seen here . A version that respects the size of the container can be seen here .

12.09.2014 / 16:48

It is possible to put the overflow on tbody only


            <td>Campo 2</td>



table {
    background-color: #aaa;
tbody {
    background-color: #ddd;
    height: 100px;
    overflow: auto;
td {
    padding: 3px 10px;


12.09.2014 / 16:16