HOWTO · HTML
How to Make an HTML Table Scrollable
Make a long HTML table scroll inside a fixed-height wrapper with CSS overflow. Choose vertical and horizontal scrolling while preserving native table markup and keyboard access.
On this page
To keep a long HTML table from stretching the page, put the semantic table inside a wrapper with a constrained height and set overflow-y: auto. The wrapper scrolls vertically when its contents are taller than that limit; the table keeps its normal row and column structure.
Make an HTML table vertically scrollable with CSS
This complete page uses a 200-pixel-high wrapper and a native table with a header and body. The rows make the content taller than the wrapper so a reader can scroll within the table area.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scrollable table</title>
<style>
.table-scroll {
max-height: 200px;
overflow-y: auto;
border: 1px solid #ccc;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
border: 1px solid #ddd;
text-align: left;
}
</style>
</head>
<body>
<div class="table-scroll" role="region" aria-label="Employee directory" tabindex="0">
<table>
<thead>
<tr><th scope="col">Name</th><th scope="col">Age</th><th scope="col">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>
The max-height keeps the wrapper bounded, while overflow-y controls what happens when the table is taller. The tabindex="0" makes the region reachable with the keyboard; focus it and use the arrow keys to check that it scrolls. The region label gives assistive technology context. Keep the <table>, <thead>, <tbody>, and header cells so browsers and assistive technology retain the table relationships.
Choose auto or scroll for vertical overflow
Use overflow-y: auto for a scrollbar when content needs to move beyond the wrapper. It avoids reserving a scrollbar when the rows fit. overflow-y: scroll requests a scrolling mechanism even when the content currently fits, though the visible scrollbar depends on the operating system and browser. In either case, put the overflow rule on the wrapper, not on individual rows. A height or maximum height is needed to establish the vertical limit; without one, the wrapper can grow with the table and no internal vertical scrolling is needed.
For a quick copy of the wrapper rule, run this small JavaScript example. It prints the CSS declarations used above; adjust the height to suit the visible area.
const rule = [
".table-scroll {",
" max-height: 200px;",
" overflow-y: auto;",
"}"
].join("\n");
console.log(rule);
.table-scroll {
max-height: 200px;
overflow-y: auto;
}
Keep table semantics and keyboard access
Use real table elements for tabular data rather than replacing them with generic CSS Grid cells. Grid is useful for layouts, but a set of div elements does not automatically expose header-to-cell relationships as a data table. The example labels the scrollable wrapper as a region and puts it in the tab order. Test it with a keyboard: tab to the labeled region, scroll with the arrow keys, then continue tabbing. Avoid adding an ARIA table role to native table markup, which already provides that semantic role.
Add horizontal scrolling and Bootstrap styling
For columns wider than the available space, add overflow-x: auto to .table-scroll; the wrapper can then scroll horizontally as well as vertically. Keep the table wide enough for its columns rather than forcing text to become unreadably narrow. Try the page at the narrow viewport where the table is used and check that every column can be reached.
Bootstrap can style the same semantic table with classes such as table and table-bordered when the project already loads Bootstrap. Those classes affect presentation; the bounded wrapper and its CSS overflow rules still provide the scrolling behavior. This approach keeps scrolling behavior explicit and lets the table markup remain useful with or without Bootstrap.