Home >Web Front-end >CSS Tutorial >How do I create a scrollable HTML table with fixed headers?

How do I create a scrollable HTML table with fixed headers?

Barbara Streisand
Barbara StreisandOriginal
2024-12-10 02:39:11962browse

How do I create a scrollable HTML table with fixed headers?

Creating a Scrollable HTML Table

In HTML, displaying a vertically scrollable table can be achieved by following these steps:

  1. Wrap the table in a div: Create a div element and set its CSS properties to have a relative position and a fixed height to limit the vertical space available for the table.
  2. Set overflow to auto: Set the div's overflow property to "auto" to enable vertical scrolling when the table content exceeds the div's height.
  3. Apply positioning to table headers: If you want the table headers (thead) to stay fixed while scrolling, set the position property of thead elements to "absolute" and position them using top, left, or right CSS properties.
  4. Set fixed width for table columns: Ensure the table columns have a fixed width to prevent them from resizing during scrolling.

Here's an example that incorporates these steps:

<div>

In this example, the "table-wrapper" div has a fixed height and an overflow set to "auto," allowing for vertical scrolling. The table headers (thead) have absolute positioning and are fixed at the top while the table body (tbody) contains the scrollable data.

By following these steps, you can create an HTML table that maintains a set size with fixed headers, while allowing the body to scroll for varying amounts of data.

The above is the detailed content of How do I create a scrollable HTML table with fixed headers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn