Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core C C#
      DotNet HTML/CSS Java JavaScript Node.js Python
      React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. HTML/CSS
  4. How to create a table with fixed header and scrollable body

How to create a table with fixed header and scrollable body

Date- Feb 10,2023

Updated Mar 2026

6420

Free Download Pay & Download

Overview of Fixed Header Tables

A fixed header table is an essential design element for any web application that requires the presentation of tabular data. It allows users to scroll through a long list of entries while keeping the headers visible, which is crucial for understanding the context of the data. This feature is commonly used in data dashboards, admin panels, and anywhere that large datasets need to be displayed efficiently.

By implementing a fixed header, users can quickly reference the column titles without having to scroll back up, thus improving the overall usability of the table. This makes it easier for users to analyze the data, especially when dealing with extensive lists of records.

In the following sections, we will provide a step-by-step guide to creating a fixed header table using HTML and CSS, along with best practices and additional features you can incorporate to enhance your tables further.

Prerequisites

Before you begin, ensure you have a basic understanding of HTML and CSS. Familiarity with how tables are structured in HTML is also beneficial. You will need a code editor to write and test your code, as well as a web browser to view the results.

Here are a few tools that can help you with this project:

  • Code Editor: Visual Studio Code, Sublime Text, or any other preferred editor.
  • Web Browser: Google Chrome, Firefox, or any modern browser with developer tools.
  • CSS Frameworks: Optional, but frameworks like Bootstrap can help you style your tables more effectively.

Creating the Basic Structure of the Table

To create a table with a fixed header, we first need to set up the basic HTML structure. Here’s how you can do it:

<!DOCTYPE html>
<html>
<head>
<style>
    table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; }
    td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; }
    tr:nth-child(even) { background-color: #dddddd; }
    .fixTableHead { position: sticky; top: 0; z-index: 9; background-color: grey; }
    .scrollableTable { height: 300px; overflow-y: auto; display: block; }
</style>
</head>
<body>
<h2>HTML Table</h2>
<div class="scrollableTable">
    <table>
        <tr class="fixTableHead">
            <th>Company</th>
            <th>Contact</th>
            <th>Country</th>
        </tr>
        <tr>
            <td>Alfreds Futterkiste</td>
            <td>Maria Anders</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>Centro comercial Moctezuma</td>
            <td>Francisco Chang</td>
            <td>Mexico</td>
        </tr>
        <tr>
            <td>Ernst Handel</td>
            <td>Roland Mendel</td>
            <td>Austria</td>
        </tr>
        <tr>
            <td>Island Trading</td>
            <td>Helen Bennett</td>
            <td>UK</td>
        </tr>
        <tr>
            <td>Laughing Bacchus Winecellars</td>
            <td>Yoshi Tannamuri</td>
            <td>Canada</td>
        </tr>
        <tr>
            <td>Magazzini Alimentari Riuniti</td>
            <td>Giovanni Rovelli</td>
            <td>Italy</td>
        </tr>
        <!-- Add more rows as needed -->
    </table>
</div>
</body>
</html>

In this example, we have created a simple table with three columns: Company, Contact, and Country. The header row is assigned the class fixTableHead, which allows it to be fixed at the top of the table. The table is wrapped in a div with the class scrollableTable, which enables vertical scrolling.

Styling the Table

Once the basic structure is in place, we can enhance the appearance of the table using CSS. Here are some styling tips to make your table more visually appealing:

  • Colors: Choose a color scheme that matches your website's theme. You can use CSS properties like background-color and color to customize the header and row styles.
  • Hover Effects: Adding hover effects to table rows can improve user interaction. Use the :hover pseudo-class to change the background color of rows when hovered.
  • Font Styles: Use font-weight, font-size, and text-align properties to enhance readability.

Here’s an example of how to add hover effects and customize colors:

.fixTableHead {
    background-color: #4CAF50;
    color: white;
}

tr:hover {
    background-color: #f1f1f1;
}

With these styles, the header will have a green background with white text, and the rows will change color when hovered over, providing a clear visual cue to users.

Edge Cases & Gotchas

When implementing a fixed header table, there are several edge cases and potential pitfalls to be aware of:

  • Browser Compatibility: Check compatibility with different browsers. The position: sticky property works well in modern browsers, but may not function as expected in older versions. Always test across multiple browsers.
  • Overflow Issues: Ensure that the parent container has a defined height and that overflow is set to auto or scroll. Otherwise, the fixed header may not behave correctly.
  • Responsive Design: Ensure your table is responsive. Use media queries to adjust the table layout for smaller screens, as fixed headers can take up valuable screen space.

Performance & Best Practices

To ensure optimal performance and maintainability when using fixed header tables, consider the following best practices:

  • Minimize DOM Manipulations: Keep the structure simple and avoid excessive nesting of elements, which can slow down rendering times.
  • Use CSS for Styling: Avoid using inline styles. Instead, use external stylesheets to manage your styles efficiently.
  • Lazy Loading: For tables with a large number of rows, consider implementing lazy loading techniques to enhance performance.
  • Accessibility: Ensure that your table is accessible to all users, including those using screen readers. Use scope attributes on th elements to describe their relationship to the data cells.

Conclusion

Creating a table with a fixed header and scrollable body is a straightforward yet powerful technique that can greatly enhance user experience. By following the steps outlined in this tutorial, you can implement this feature in your own projects effectively.

Key Takeaways:

  • Fixed header tables improve usability by keeping column headers visible during scrolling.
  • Ensure cross-browser compatibility and test thoroughly in different environments.
  • Implement best practices for performance, including minimizing DOM manipulations and ensuring accessibility.
  • Enhance the appearance of your tables with CSS for a better user interface.
table

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Responsive Slick Slider
Jul 28, 2022
How to add a WhatsApp share button on a website
Aug 22, 2022
Slick Slider with single slide
May 09, 2021
Code syntax higlighter using Prism js
Dec 12, 2020
Previous in HTML/CSS
How to add a WhatsApp share button on a website
Next in HTML/CSS
Free PHP, HTML, CSS, JavaScript/TypeScript editor - CodeLobster I…

Comments

On this page

🎯

Interview Prep

Ace your HTML/CSS interview with curated Q&As for all levels.

View HTML/CSS Interview Q&As

More in HTML/CSS

  • Create a Music Player in HTML/CSS: Step-by-Step Guide 9352 views
  • Free PHP, HTML, CSS, JavaScript/TypeScript editor - CodeLobs… 4927 views
  • Child internet safety 4326 views
  • Complete Guide to Creating a Registration Form in HTML/CSS 4037 views
  • Mastering Chapter 1 CSS: A Complete Guide with Examples in H… 3695 views
View all HTML/CSS posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor