html - How To: 100% Height Table only Scroll tbody

Html - How To: 100% Height Table only Scroll tbody

To create a table where only the tbody scrolls and the thead remains fixed with a height of 100% of its container, you can use a combination of HTML and CSS. Here's how you can do it:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Scrollable tbody</title> <style> .table-container { height: 300px; /* Set the height of the container */ overflow-y: auto; /* Add vertical scrollbar when tbody overflows */ border: 1px solid #ccc; } table { width: 100%; border-collapse: collapse; } thead, tbody { display: block; } tbody { height: calc(100% - 30px); /* Subtract the height of the thead */ overflow-y: scroll; /* Add scrollbar for tbody */ } th, td { padding: 8px; border: 1px solid #ddd; } </style> </head> <body> <div class="table-container"> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <!-- Add more rows as needed --> </tbody> </table> </div> </body> </html> 

In this example:

  • The container div .table-container has a fixed height and overflow set to auto, which enables scrolling when the content exceeds the container's height.
  • The table itself has its width set to 100% to fit the container.
  • Both the thead and tbody are set to display block, which allows them to be styled independently.
  • The tbody is given a fixed height (calculated as 100% of the container height minus the height of the thead) and overflow set to scroll, so it scrolls independently of the thead.
  • Finally, the th and td elements are styled with padding and border to resemble a typical table layout.

You can adjust the height of the container and other styles as needed to fit your design requirements.

Examples

  1. "CSS 100% height table with scrolling tbody" Description: This query typically seeks solutions for creating a table where only the body section scrolls, while the header and footer remain fixed. Developers often use CSS techniques to achieve this layout.

    <div class="table-container"> <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> <tfoot> <!-- Footer content here --> </tfoot> </table> </div> 
    .table-container { height: 300px; /* Set desired height */ overflow-y: auto; } 
  2. "HTML table with fixed header and scrolling body" Description: This query is about creating a table layout where the header remains fixed while the body section scrolls. It involves CSS to set up the desired fixed and scrolling behavior.

    <div class="table-container"> <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> </div> 
    .table-container { height: 300px; /* Set desired height */ overflow-y: auto; } thead, tbody { display: block; } tbody { height: 200px; /* Set desired body height */ overflow-y: auto; } 
  3. "HTML table scrollable tbody without fixed height" Description: This query looks for a solution to make the tbody scrollable without setting a fixed height. Developers often aim for dynamic height based on content.

    <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> 
    tbody { display: block; height: 200px; /* Set desired height or max-height */ overflow-y: auto; } 
  4. "Responsive HTML table with scrolling tbody" Description: This query indicates a need for a table layout that adapts to different screen sizes and devices while allowing the tbody to scroll when necessary.

    <div class="table-container"> <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> </div> 
    .table-container { max-height: 300px; /* Set max-height for responsiveness */ overflow-y: auto; } 
  5. "HTML table with scrollable tbody using jQuery" Description: This query suggests the use of jQuery along with HTML and CSS to achieve a scrollable tbody within a table.

    <table id="scrollable-table"> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> 
    $(document).ready(function() { $('#scrollable-table tbody').css('overflow-y', 'auto'); }); 
  6. "How to make tbody scrollable in HTML table without fixed height" Description: This query is similar to previous ones, emphasizing achieving a scrollable tbody without specifying a fixed height.

    <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> 
    tbody { display: block; max-height: 300px; /* Set max-height as needed */ overflow-y: auto; } 
  7. "Creating a table with only tbody scrollable in HTML" Description: Here, the focus is on creating a table where only the tbody section scrolls while the header and footer remain fixed.

    <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> <tfoot> <!-- Footer content here --> </tfoot> </table> 
    tbody { display: block; max-height: 300px; /* Set max-height as needed */ overflow-y: auto; } 
  8. "HTML table tbody scroll without fixed height using flexbox" Description: This query seeks a solution utilizing CSS flexbox to create a scrollable tbody without specifying a fixed height.

    <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> 
    tbody { display: block; max-height: 300px; /* Set max-height as needed */ overflow-y: auto; } 
  9. "Scrollable tbody in HTML table with variable height content" Description: This query suggests a need for a solution where the height of the tbody adjusts dynamically based on the content while allowing scrolling.

    <table> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> 
    tbody { display: block; max-height: 300px; /* Set max-height as needed */ overflow-y: auto; } 
  10. "Creating an HTML table with scrollable tbody using CSS grid" Description: This query explores using CSS grid to create a table layout where only the tbody scrolls.

    <table class="scrollable-table"> <thead> <!-- Header content here --> </thead> <tbody> <!-- Body content here --> </tbody> </table> 
    .scrollable-table { display: grid; grid-template-rows: auto 1fr; /* Header auto-sized, tbody fills remaining space */ max-height: 300px; /* Set max-height as needed */ overflow-y: auto; } 

More Tags

confidence-interval axes xamarin multi-user window-resize client-side dateinterval double basic mysql-error-1064

More Programming Questions

More Livestock Calculators

More Animal pregnancy Calculators

More Auto Calculators

More Tax and Salary Calculators