The Wayback Machine - https://web.archive.org/web/20240801052259/https://www.geeksforgeeks.org/how-to-create-a-table-with-fixed-header-and-scrollable-body/
Open In App

How to create a table with fixed header and scrollable body ?

Last Updated : 25 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The purpose of this article is to create a table with a fixed header and scrollable body. We can create such a table with either of the two approaches.

  1. By setting the position property to “sticky” and specifying “0” as a value of the top property for the <th> element.
  2. By setting the display to “block” for both <thead> and <tbody> element so that we can apply the height and overflow properties to <tbody>.

Note: 

  • The position property states the type of positioning for an element.
  • The display property states that how an element should be displayed.

Example: Below is the code which illustrates the formation of table with position property.

HTML




<!DOCTYPE html>
<html>
  
<head>
  <style>
    .fixTableHead {
      overflow-y: auto;
      height: 110px;
    }
    .fixTableHead thead th {
      position: sticky;
      top: 0;
    }
    table {
      border-collapse: collapse;        
      width: 100%;
    }
    th,
    td {
      padding: 8px 15px;
      border: 2px solid #529432;
    }
    th {
      background: #ABDD93;
    }
  </style>
</head>
  
<body>
  <div class="fixTableHead">
    <table>
      <thead>
        <tr>
          <th>Col 1</th>
          <th>Col 2</th>
        </tr>
      </thead>
  
      <tbody>
        <tr>
          <td>1.1</td>
          <td>2.1</td>
        </tr>
        <tr>
          <td>1.2</td>
          <td>2.2</td>
        </tr>
        <tr>
          <td>1.3</td>
          <td>2.3</td>
        </tr>
        <tr>
          <td>1.4</td>
          <td>2.4</td>
        </tr>
        <tr>
          <td>1.5</td>
          <td>2.5</td>
        </tr>
      </tbody>
        
    </table>
  </div>
</body>
  
</html>


Output:

Example 2: Below is the code which illustrates the formation of table with display property.

HTML




<!DOCTYPE html>
<html>
  
<head>
  <style>
    .tableFixHead {
      width: 500px;
      table-layout: fixed;
      border-collapse: collapse;
    }
      .tableFixHead tbody {
      display: block;
      width: 100%;
      overflow: auto;
      height: 50px;
    }
    .tableFixHead thead tr {
      display: block;
    }
    .tableFixHead th,
    .tableFixHead  td {
      padding: 5px 10px;
      width: 200px;
    }
    th {
      background: #ABDD93;
    }
  </style>
</head>
  
<body>
  <div class="tableFixHead">
    <table>
      <thead>
        <tr>
          <th>Col 1</th>
          <th>Col 2</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1.1</td>
          <td>2.1</td>
        </tr>
        <tr>
          <td>1.2</td>
          <td>2.2</td>
        </tr>
        <tr>
          <td>1.3</td>
          <td>2.3</td>
        </tr>
        <tr>
          <td>1.4</td>
          <td>2.4</td>
        </tr>
        <tr>
          <td>1.5</td>
          <td>2.5</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
  
</html>


Output:



Previous Article
Next Article

Similar Reads

How to Fixed One Column and Scrollable Other Column or Columns in Bootstrap?
Bootstrap provides several built-in features to the users. This makes development faster and smoother. However, in some cases, external CSS and jQuery has to be implemented to the web page's coding in order to give it a personalized touch. Usually, the Bootstrap grid is used to divide the web-page into 12 equal columns. These columns can be grouped
4 min read
Angular PrimeNG Table Column Widths of a Scrollable Table
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Table Column Widths of a Scrollable Table in Angular PrimeNG. Angular PrimeNG Table Column Widths of a Scrollable
5 min read
How to create Table with 100% width, with Vertical Scroll inside Table body in HTML?
To add a vertical scroll bar inside the table body, use the overflow-y property. By changing the display property of tbody to block, we can display the block-level element. However, when changing the display property of tbody, we must also change the property of thead to prevent the table layout from breaking. ApproachApply the class scroll down to
2 min read
How to Create a Fixed/Sticky Header on Scroll with CSS and JavaScript?
A fixed or sticky header remains at the top of the webpage when the user scrolls down. This functionality enhances navigation and user experience by keeping important links always visible. In this article, we will explore the approach to creating a fixed/sticky header on scroll using CSS and JavaScript. ApproachCreate a basic HTML structure with a
3 min read
Blaze UI Cards Header Body and Footer
Blaze UI is a CSS open-source framework. It is a lightweight UI toolkit and provides great tools for building customized and scalable applications. It can work with any framework that exists. It can adapt to any ecosystem. All designs or CSS are mobile-first and hence responsive. It project is available open-source so a large community maintains it
2 min read
How to Set Offset an HTML Anchor Element to Adjust Fixed Header in CSS ?
When designing a website with a fixed header, we might face an issue where clicking on a link obscures the target element due to the header. This affects the user experience negatively as the content gets hidden. In this article, we will see how to offset HTML anchors for a fixed header, ensuring proper visibility for users. HTML anchors, also call
5 min read
Offsetting an anchor to adjust for fixed header
In this article, we will see how to Offset an anchor to adjust for a fixed header on a web page By using different approaches. When we build websites, it's common to use fixed headers that stay at the top of the page even if the user scrolls the page. However, this can cause issues with anchor links, which are used to quickly jump to a specific sec
4 min read
How to Click on header to add another header in jQuery ?
In this article, we will see how to add a new header element after clicking the existing header elements in jQuery. To add the new header element, the delegate() method is used. Syntax: $(selector).delegate("Selected_elements", function() { $(this).after("content"); }) Here, we use delegate() method to add one or more event handlers to the specifie
1 min read
How to define whether a header cell is a header for a column, row, or group of columns or rows in HTML 5?
In this article, we will learn how to specify whether a header cell is a header of a column, row, or group of columns or rows. The &lt;th&gt; tag is used to specify the heading for an &lt;table&gt; element, typically for the columns of the table. However, in specific cases, the header can be used to define the heading of a row or a group of columns
2 min read
How to create horizontal scrollable sections using CSS ?
In this article, we will see how we can create a horizontal scrollable section using CSS. We are going to make different sections and make them horizontally scrollable using CSS. HTML code is used to create the basic structure of the sections and CSS code is used to set the style. HTML Code: In this section, we will create a structure for our secti
3 min read
three90RightbarBannerImg