How to Make HTML Table Scrollable

  1. Method 1: Using CSS Overflow Property
  2. Method 2: Using CSS Grid Layout
  3. Method 3: Using Bootstrap for Scrollable Tables
  4. Conclusion
  5. FAQ
How to Make HTML Table Scrollable

Creating a scrollable HTML table is a common requirement in web development, especially when dealing with large datasets. A scrollable table enhances user experience by allowing users to navigate extensive information without overwhelming the page layout. In this tutorial, we will explore how to make an HTML table vertically scrollable, ensuring that your data remains accessible and organized.

Whether you’re building a dashboard, a data visualization tool, or simply displaying tabular data, a scrollable table can be an essential feature. We will cover various methods to achieve this, focusing on CSS techniques that can be easily implemented alongside your HTML structure. Let’s dive into the details and make your tables more user-friendly!

Method 1: Using CSS Overflow Property

One of the simplest ways to make an HTML table scrollable is by utilizing the CSS overflow property. This method allows you to define a specific height for your table container and enable scrolling when the content exceeds that height.

Here’s a straightforward example to illustrate this approach:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scrollable HTML Table</title>
    <style>
        .scrollable-table {
            width: 100%;
            height: 200px;
            overflow-y: auto;
            border: 1px solid #ccc;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
    </style>
</head>
<body>
    <div class="scrollable-table">
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Country</th>
                </tr>
            </thead>
            <tbody>
                <tr><td>John Doe</td><td>30</td><td>USA</td></tr>
                <tr><td>Jane Smith</td><td>25</td><td>Canada</td></tr>
                <tr><td>Sam Wilson</td><td>28</td><td>UK</td></tr>
                <tr><td>Emily Johnson</td><td>35</td><td>Australia</td></tr>
                <tr><td>Michael Brown</td><td>40</td><td>Germany</td></tr>
                <tr><td>Linda Davis</td><td>22</td><td>France</td></tr>
                <tr><td>James Garcia</td><td>29</td><td>Spain</td></tr>
                <tr><td>Patricia Martinez</td><td>31</td><td>Mexico</td></tr>
                <tr><td>Robert Wilson</td><td>38</td><td>Italy</td></tr>
                <tr><td>Jessica Lee</td><td>27</td><td>Netherlands</td></tr>
            </tbody>
        </table>
    </div>
</body>
</html>

In this code, we create a div with a class of scrollable-table that wraps around the table. The CSS sets a fixed height of 200 pixels and enables vertical scrolling with the overflow-y: auto property. When the number of rows exceeds the defined height, users can scroll through the table without affecting the overall layout of the page. This method is efficient and works seamlessly across different browsers.

Method 2: Using CSS Grid Layout

Another effective method for creating a scrollable HTML table is by using the CSS Grid layout. This approach provides more flexibility and control over the table’s structure and appearance. Here’s how you can implement it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scrollable HTML Table with CSS Grid</title>
    <style>
        .grid-table {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            width: 100%;
            height: 200px;
            overflow-y: auto;
            border: 1px solid #ccc;
        }
        .grid-table div {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .header {
            background-color: #f2f2f2;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="grid-table">
        <div class="header">Name</div>
        <div class="header">Age</div>
        <div class="header">Country</div>
        <div>John Doe</div>
        <div>30</div>
        <div>USA</div>
        <div>Jane Smith</div>
        <div>25</div>
        <div>Canada</div>
        <div>Sam Wilson</div>
        <div>28</div>
        <div>UK</div>
        <div>Emily Johnson</div>
        <div>35</div>
        <div>Australia</div>
        <div>Michael Brown</div>
        <div>40</div>
        <div>Germany</div>
        <div>Linda Davis</div>
        <div>22</div>
        <div>France</div>
        <div>James Garcia</div>
        <div>29</div>
        <div>Spain</div>
        <div>Patricia Martinez</div>
        <div>31</div>
        <div>Mexico</div>
        <div>Robert Wilson</div>
        <div>38</div>
        <div>Italy</div>
        <div>Jessica Lee</div>
        <div>27</div>
        <div>Netherlands</div>
    </div>
</body>
</html>

In this implementation, we use a grid layout to define the structure of our table. Each cell is a div, and we specify the grid columns using grid-template-columns. The overall height is again fixed at 200 pixels, and scrolling is enabled with overflow-y: auto. This method allows for more complex layouts and can be easily customized to fit your design needs.

Method 3: Using Bootstrap for Scrollable Tables

If you’re using Bootstrap in your project, creating a scrollable table becomes even easier. Bootstrap provides built-in classes that can help you achieve this effect without writing much custom CSS. Here’s how to do it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scrollable HTML Table with Bootstrap</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        .table-container {
            height: 200px;
            overflow-y: auto;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Country</th>
                </tr>
            </thead>
            <tbody>
                <tr><td>John Doe</td><td>30</td><td>USA</td></tr>
                <tr><td>Jane Smith</td><td>25</td><td>Canada</td></tr>
                <tr><td>Sam Wilson</td><td>28</td><td>UK</td></tr>
                <tr><td>Emily Johnson</td><td>35</td><td>Australia</td></tr>
                <tr><td>Michael Brown</td><td>40</td><td>Germany</td></tr>
                <tr><td>Linda Davis</td><td>22</td><td>France</td></tr>
                <tr><td>James Garcia</td><td>29</td><td>Spain</td></tr>
                <tr><td>Patricia Martinez</td><td>31</td><td>Mexico</td></tr>
                <tr><td>Robert Wilson</td><td>38</td><td>Italy</td></tr>
                <tr><td>Jessica Lee</td><td>27</td><td>Netherlands</td></tr>
            </tbody>
        </table>
    </div>
</body>
</html>

Output:

In this example, we include Bootstrap’s CSS for styling and use its built-in classes to create a table. The table-container class wraps around the table and sets a fixed height with vertical scrolling enabled. This method is especially useful if you want to maintain a consistent look and feel across your web application while leveraging Bootstrap’s responsive design capabilities.

Conclusion

Making an HTML table scrollable is a straightforward process that can significantly enhance the usability of your web applications. By using CSS properties, CSS Grid, or Bootstrap classes, you can create an organized and accessible way to present large datasets. Choose the method that best fits your project requirements, and enjoy the benefits of a cleaner, more user-friendly interface.

Incorporating scrollable tables not only improves user experience but also keeps your design neat and responsive. Whether you’re working on a data-heavy application or simply want to showcase information effectively, these techniques will serve you well.

FAQ

  1. How do I make a horizontal scrollable table?
    You can make a table horizontally scrollable by setting a fixed width for the table container and using the overflow-x: auto CSS property.

  2. Can I use JavaScript to create a scrollable table?
    Yes, you can use JavaScript to dynamically set the height or width of a table container and enable scrolling based on user interactions.

  3. Are there any accessibility considerations for scrollable tables?
    Yes, ensure that your scrollable tables are keyboard navigable and consider using ARIA roles to enhance accessibility for screen reader users.

  4. What browsers support scrollable tables?
    Most modern browsers, including Chrome, Firefox, Safari, and Edge, support CSS properties like overflow used to create scrollable tables.

  5. Can I style the scrollbar in a scrollable table?
    Yes, you can customize the scrollbar using CSS properties like ::-webkit-scrollbar for webkit browsers, allowing for a more tailored design.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

Related Article - HTML Table