Horizontal Scrollbar for Tables

To display horizontal scrollbar for tables using CSS, enclose the table in a div, and set the div’s CSS property overflow-x to auto;

The HTML code to enclose a table in a div is shown in the following.

<div id="tablediv">
  <table>
    <!-- table content -->
  </table>
</div>

The CSS code to set the overflow-x property for the enclosing div is

#tablediv {
  overflow-x: auto;
}

Examples

ADVERTISEMENT

1. Horizontal Scrollbar for Table

In the following example, we take a table with five columns, and width set to 1500px. If this width is greater than the allowed width for the enclosing div on your screen, then the enclosing div allows the table to overflow in x-axis direction, enabling the horizontal scrollbar.

index.html

Conclusion

In this CSS Tutorial, we learned how to enable horizontal scrollbar when the width of the table is more than what we want, with examples.