Open In App

How to create Table with 100% width, with Vertical Scroll inside Table body in HTML?

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To create a table with 100% width and a vertical scroll inside the table body in HTML, use the overflow-y property. Set the <tbody> display to block to enable scrolling while adjusting the <thead> to maintain the layout.

This method creates a scrollable table for effectively displaying large datasets, enhancing user experience with an HTML table scrollable design.

Approach

  • Apply the class scroll down to the table for styling.
  • Set width: 100% to make the table span the entire width.
  • Enable vertical scrolling in the tbody with height: 50px and overflow-y: auto.
  • Style columns with a fixed width (width: 200px) and borders.
  • Improve header appearance with a fixed height and centered content.

Example: In this example, we will see how to create a table with 100% width, with a vertical scroll inside the table body in HTML.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        Display table with vertical scrollbar
    </title>

    <style>
        table.scrolldown {
            width: 100%;

            /* border-collapse: collapse; */
            border-spacing: 0;
            border: 2px solid black;
        }

        /* To display the block as level element */
        table.scrolldown tbody,
        table.scrolldown thead {
            display: block;
        }

        thead tr th {
            height: 40px;
            line-height: 40px;
        }

        table.scrolldown tbody {

            /* Set the height of table body */
            height: 50px;

            /* Set vertical scroll */
            overflow-y: auto;

            /* Hide the horizontal scroll */
            overflow-x: hidden;
        }

        tbody {
            border-top: 2px solid black;
        }

        tbody td,
        thead th {
            width: 200px;
            border-right: 2px solid black;
        }

        td {
            text-align: center;
        }
    </style>
</head>

<body>
    <table class="scrolldown">

        <!-- Table head content -->
        <thead>
            <tr>
                <th>Heading 1</th>
                <th>Heading 2</th>
                <th>Heading 3</th>
                <th>Heading 4</th>
                <th>Heading 5</th>
            </tr>
        </thead>

        <!-- Table body content -->
        <tbody>
            <tr>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
            </tr>

            <tr>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
            </tr>
            <tr>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
            </tr>
            <tr>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
            </tr>
            <tr>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
                <td>Content</td>
            </tr>
        </tbody>
    </table>

    <body>

</html>

Output:

hyu



Next Article

Similar Reads