How to set tbody height with overflow scroll

Updated at 30-May-2021, By samar

How to set tbody height with overflow scroll

Through the use of the programming language, we will work together to solve the "How to set tbody height with overflow scroll" puzzle in this lesson.

Sometimes we have to set the table tbody height and scroll the overflow content. In that case we can fix the table tbody height and scroll the tbody content within tbody using the overflow property.
  • Html table tbody with fixed height and scrollbar

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Scrollab</title>
        <style type="text/css" scoped>
            table{
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width:600px;
            }
            table td, th{
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }
    
            table tbody{
                display:block;
                height:200px;
                overflow:auto;
            }
            table thead, tbody tr {
                display:table;
                width:100%;
                table-layout:fixed;
            }
            table thead {
                width: calc( 100% - 1em )
            }
        </style>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>Header Cell 1</th>
                    <th>Header Cell 2</th>
                    <th>Header Cell 3</th>
                    <th>Header Cell 4</th>
                    <th>Header Cell 5</th>
                    <th>Header Cell 6</th>
                    <th>Header Cell 7</th>
                    <th>Header Cell 8</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Row 1, Cell 1</td>
                    <td>Row 1, Cell 2</td>
                    <td>Row 1, Cell 3</td>
                    <td>Row 1, Cell 4</td>
                    <td>Row 1, Cell 5</td>
                    <td>Row 1, Cell 6</td>
                    <td>Row 1, Cell 7</td>
                    <td>Row 1, Cell 8</td>
                </tr>
                <tr>
                    <td>Row 2, Cell 1</td>
                    <td>Row 2, Cell 2</td>
                    <td>Row 2, Cell 3</td>
                    <td>Row 2, Cell 4</td>
                    <td>Row 2, Cell 5</td>
                    <td>Row 2, Cell 6</td>
                    <td>Row 2, Cell 7</td>
                    <td>Row 2, Cell 8</td>
                </tr>
                <tr>
                    <td>Row 3, Cell 1</td>
                    <td>Row 3, Cell 2</td>
                    <td>Row 3, Cell 3</td>
                    <td>Row 3, Cell 4</td>
                    <td>Row 3, Cell 5</td>
                    <td>Row 3, Cell 6</td>
                    <td>Row 3, Cell 7</td>
                    <td>Row 3, Cell 8</td>
                </tr>
                <tr>
                    <td>Row 4, Cell 1</td>
                    <td>Row 4, Cell 2</td>
                    <td>Row 4, Cell 3</td>
                    <td>Row 4, Cell 4</td>
                    <td>Row 4, Cell 5</td>
                    <td>Row 4, Cell 6</td>
                    <td>Row 4, Cell 7</td>
                    <td>Row 4, Cell 8</td>
                </tr>
                <tr>
                    <td>Row 5, Cell 1</td>
                    <td>Row 5, Cell 2</td>
                    <td>Row 5, Cell 3</td>
                    <td>Row 5, Cell 4</td>
                    <td>Row 5, Cell 5</td>
                    <td>Row 5, Cell 6</td>
                    <td>Row 5, Cell 7</td>
                    <td>Row 5, Cell 8</td>
                </tr>
            </tbody>
        </table>
    </body>
    </html>
    

    To fix the HTML table tbody height with scrollable content you have to fix height with overflow auto property. For table thead, tbody tr you have to set display table and table-layout fixed properties.

Back to code snippet queries related html