What is the best way to add a scrollable tbody section to an HTML table?

I am encountering an issue while attempting to make the tbody section of an HTML table scrollable.

Consider this basic HTML table:

<table border="1" class="scrollableTable">
    <thead>
        <tr>
            <th width="14.2%">RM Code</th>
            <th width="14.2%">Author Signature</th>
            <th width="14.2%">Signature Date</th>
            <th width="14.2%">Acquisition Reserve</th>
            <th width="14.2%">Final Delivery</th>
            <th width="14.2%">RM Description</th>
            <th width="14.2%">Taxable Amount</th>
         </tr>
     </thead>

     <tbody>
         <tr class="even" id="rmRow">
             <td width="14.2%">0001</td>
             <td width="14.2%">bla</td>
             <td width="14.2%">00000000</td>
             <td width="14.2%">bla</td>
             <td width="14.2%">bla</td>
             <td width="14.2%">19/09</td>
             <td width="14.2%">57.0</td>
          </tr>

         <tr class="even" id="rmRow">
             <td width="14.2%">0002</td>
             <td width="14.2%">bla</td>
             <td width="14.2%">00000000</td>
             <td width="14.2%">bla</td>
             <td width="14.2%">bla</td>
             <td width="14.2%">19/09</td>
             <td width="14.2%">57.0</td>
         </tr>

         // More rows ...

       </tbody>
</table>

In the tbody section, there are multiple rows that could exceed the available height. To address this, I want to set a specific height for this section and make it scrollable to view all rows.

To achieve this, I applied these CSS settings:

  1. Firstly, I changed the display of tbody to block in order to assign a height to it.

  2. Next, I specified a height for the tbody section.

  3. Finally, I added vertical overflow as scroll to enable scrolling within the tbody section.

This is the CSS code snippet:

tbody {
    display: block;
    height: 100px;
    overflow-y: scroll;
}

Despite my efforts, the result does not align properly and looks messy. The columns in the tbody do not match with the thead.

Any insights on why this is happening? How can I rectify this issue?

Thank you

Answer №1

To ensure that the cells in your tbody align with those in the thead, you need to apply display: table-row-group; to the tbody. This prevents setting a specific height for the tbody, requiring a workaround.

You can enable scrolling for the rows within the tbody by using overflow-y: scroll; on the table. However, this may also cause the thead to scroll along. To prevent this, consider fixing the position of the thead and adjusting its display to maintain a static appearance at the top of the table while allowing the rest of the content to scroll.

I have made modifications to your JSFiddle, incorporating the following CSS:

tbody {
    margin-top: 40px;
}

table {
    display: block;
    height: 100px;
    overflow-y: scroll;
    position: relative;
    margin-top: 50px;
}
thead {
    background: white;
    position: fixed;
    top: 0;
    margin-right: 25px;
    display: table-row-group;
}
th, td {
    width: 80px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I adjust this button to match the size of the input field?

I'm in the process of creating a simple landing page and I've encountered an issue with the newsletter signup component. The button in the bootstrap input group template appears much larger than the input field. Here is the current code snippet: ...

Is there a way for me to recreate the appearance of the outline and labeling found in Material-UI's outlined textfield?

Can anyone help me figure out how to hide the border behind the title text in an outlined textfield from Material-UI that I am trying to imitate? In the image below, you can see "Due Date/Time" taken from Material-UI library where the title hides the bord ...

What is the best way to ensure text starts on a new line when paragraphs and images are floating and aligned?

Does anyone have a clearer title idea? Feel free to edit. To better illustrate my point, I've created a jsfiddle. I am working with a simple layout of text and images. My goal is to make the second text and image appear on a new row just below the f ...

Tips for setting up a hierarchical mat-table within a parent table that supports expandable rows using Angular Material

Here is the data that I am working with: [ { "_id": "c9d5ab1a", "subdomain": "wing", "domain": "aircraft", "part_id": "c9d5ab1a", "info.mimetype": "application/json", "info.dependent": "parent", ...

Introducing the new default in Google Chrome: Now accessing window.opener for target="_blank" links after rel="noopener" is the standard feature

Exploring a new attribute called rel="noopener", I discovered that it keeps opened windows unaware of their opener. After some experimentation, I consistently found window.opener === null, only to realize that this is now the default behavior in ...

Eliminate extraneous space with the clearfix:after pseudo-element

Could use some help figuring out how to remove the unwanted whitespace caused by clearing the float (specifically after the tabs). Any suggestions on how to fix this issue? Take a look at the code snippet below (jsfiddle): /* Clearfix */ .clearfix:af ...

Is it possible to detach keyboard events from mat-chip components?

Is there a way to allow editing of content within a mat-chip component? The process seems simple in HTML: <mat-chip contenteditable="true">Editable content</mat-chip> Check out the StackBlitz demo here While you can edit the content within ...

I'm facing an issue where I am unable to access an element by its ID in a c++ application, despite everything else functioning correctly

I am currently working on some HTML, CSS, and JS tasks and I have encountered a problem. For some reason, I am unable to access elements in JavaScript using getElementById or jQuery tags... nothing seems to work. Here is the HTML code for my element: < ...

Query on HTML Styling

While I am relatively new to HTML programming, I have previous experience with languages like C++ and C#. Recently, I found a code snippet online for a timer and decided to customize it for my needs. The original code only accepted numbers for minutes an ...

The email submission function in my PHP form seems to be malfunctioning. I must be overlooking something

Having an issue with my PHP form, it's only sending field names instead of data to the email address. Here is a simplified version of the code: <?php $ToEmail = 'example@example.com'; $EmailSubject = 'Site contact form'; $ma ...

In Angular, encountering difficulty accessing object members within an array when using custom pipes

Here is a custom pipe that I have created, but I am facing an issue accessing the members of the customfilter array, which is of type Item. import { Pipe, PipeTransform } from '@angular/core'; import {Bus} from '/home/pavan/Desktop/Pavan ...

Using CSS styling to dictate how browsers display web content, specifically through setting font size measurements in EM

On different mobile devices and various browsers such as Internet Explorer and Mozilla, the font size in some divs does not appear consistent. Specifically, on a Samsung Galaxy S6. I am using EM units: I know it may not be ideal but I prefer this method a ...

Having trouble modifying a value in a form and submitting it to the following jsp page

I'm encountering an issue with changing the value in a hidden input element before submitting data to another JSP file (db.jsp) for processing. The value should be different depending on which button is clicked, but it always remains as the default va ...

24-hour countdown tool featuring a visual progress bar

Currently, I am in the process of developing an application aimed at assisting individuals in either forming new habits or breaking old ones. In this application, I am looking to implement a countdown timer that ticks down daily; with the help of this help ...

There seems to be a malfunction with the hide and reset features, as they are

I have encountered an issue while working on hiding a series in Google Charts when clicked on the legend. The problem arises when an extra column is added to display tooltips on the bars, causing the functionality to break. Please check out the demos below ...

"An issue with preventing default behavior when clicking on a jQuery element

I need some assistance preventing a JQuery onclick function from scrolling the page to the top. I have tried using preventDefault() without success. You can find the code on this website: Below is the code snippet that I am currently using: $( document ...

What is the method for extending the dropdown of a fixed width select box to the left rather than the right?

Here is the code snippet I'm currently working with: http://jsfiddle.net/9pyh2/ <style type="text/css> select {width:200px;} </style> <form> <select><option value="">Choose one</option> <option>F ...

Get the title text from a selected div and create an array using JQuery

I'm attempting to gather the text from all divs' Title Attributes and store them in an array, but I'm having trouble finding the correct selection. The title is important because it holds the specific string that I require. Below are three ...

The DataGrids effortlessly expanded to accommodate their parents without the need for a horizontal scrollbar, showcasing their full capacity

After conducting numerous tests on simpler models, I have come to realize that the display of DataGrids Pro has been altered in MUI v6, rather than being a result of something I had broken initially. My workplace utilizes multiple DataGrids with columns h ...

Adaptable and personalized Timeline

I created a timeline but unfortunately, I am facing some challenges that I cannot seem to resolve :( Firstly, the timeline is not adapting to different screen sizes, causing the lines to separate when the screen is enlarged or when longer descriptions are ...