What is the best way to ensure a table is responsive while maintaining a fixed header? This would involve the table scrolling when it reaches the maximum viewpoint, while also keeping

Can anyone help me create a responsive table with a fixed header that scrolls when it reaches the maximum viewpoint without scrolling the entire page? I've tried using box-sizing: border-box; and overflow-x:scroll; but it didn't work. Any suggestions would be much appreciated. Thank you.

table{
    border-collapse: separate;
    border-spacing: 0;
    width: 100%;
    box-sizing: border-box;
}
thead,tbody{
box-sizing: border-box;
overflow: auto;
}
th,td{
    padding: 6px 15px;
}
th{
    background: #42444e;
    color: #fff;
    text-align: left;
    position: static;
    top: 50px;
}
tbody tr td img{
    flex-wrap: wrap;
    pointer-events: none;       
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;

    width: 30px;
    height: 30px;
    float: none;
    display:block;
    object-fit: fill;
    border-radius: 10%;
}
tr:first-child th:first-child {
  border-top-left-radius: 6px;
}
tr:first-child th:last-child {
  border-top-right-radius: 6px;
}
td{
    border-right: 1px solid #c6c9cc;
    border-bottom: 1px solid #c6c9cc;
}
td:first-child {
  border-left: 1px solid #c6c9cc;
}
tr:nth-child(even) td {
  background: #eaeaed;
}
tr:last-child td:first-child {
  border-bottom-left-radius: 6px;
}
tr:last-child td:last-child {
  border-bottom-right-radius: 6px;
}
<table>
        <thead>
            <tr>
                <th>Image</th>
                <th>ID</th>
                <th>Date</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone no.</th>
                <th>Role</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><img src="img/4.jpeg"></td>
                <td>1</td>
                <td>445445564</td>
                <td>Umann goswami</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dc89b1bdb2b2bbb3afabbdb1b59cbbb1bdb5b0f2bfb3b1">[email protected]</a></td>
                <td>9999672450</td>
                <td>Admin</td>
            </tr>
                        <tr>
                <td><img src="img/4.jpeg"></td>
                <td>1</td>
                <td>445445564</td>
                <td>Umann goswami</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="045169656a6a636b777365696d446369656d682a676b69">[email protected]</a></td>
                <td>9999672450</td>
                <td>Admin</td>
            </tr>
                        <tr>
                <td><img src="img/4.jpeg"></td>
                <td>1</td>
                <td>445445564</td>
                <td>Umann goswami</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f1a222e212128203c382e22260f28222e2623612c2022">[email protected]</a></td>
                <td>9999672450</td>
                <td>Admin</td>
            </tr>
                        <tr>
                <td><img src="img/4.jpeg"></td>
                <td>1</td>
                <td>445445564</td>
                <td>Umann goswami</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85d0e8e4ebebe2eaf6f2e4e8ecc5e2e8e4ece9abe6eae8">[email protected]</a></td>
                <td>9999672450</td>
                <td>Admin</td>
            </tr>
        </thead>
    </table>

Answer №1

After carefully reviewing your codepan, I have determined that you need to incorporate additional CSS properties into your CSS file in order for it to function properly.

tbody {
    height: 200px;
    display: inline-block;
    overflow: auto;
}
thead tr th{
  position: sticky;
  top:0;
  left:0;
  right:0;
}

Answer №2

  1. adjust the header to be sticky
  2. make sure table overflow-y is set to auto

    .fixTable {
      overflow-y: auto;
      height: 110px;
    }
    .fixTable thead th {
      position: sticky;
      top: 0;
    }
    table {
      border-collapse: collapse;        
      width: 100%;
    }
    th,
    td {
      padding: 8px 15px;
      border: 2px solid #529432;
    }
    th {
      background: #060606;
    }
    ::-webkit-scrollbar {
        -webkit-appearance: none;
    }
    ::-webkit-scrollbar:vertical {
        width: 12px;
    }
    ::-webkit-scrollbar:horizontal {
        height: 12px;
    }
    ::-webkit-scrollbar-thumb {
        background-color: rgba(0, 0, 0, .5);
        border-radius: 10px;
        border: 2px solid #ffffff;
    }
    ::-webkit-scrollbar-track {
        border-radius: 10px;
        background-color: #ffffff;
    }
<!DOCTYPE html>
<html>
  
<head>
  
</head>
  
<body>
  <div class="fixTable">
    <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>

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

Align the top navigation centered horizontally, with the logo on the left and language selection on the right

Is there a way to center my menu horizontally while keeping the logo aligned to the left and language selection to the right, all with consistent proportions when resizing? I've tried various suggestions but nothing seems to work for my navigation bar ...

Ensure that the headers of the table are in perfect alignment with the

Is there a way to create a table with fixed row headers so that column headings stay in place while scrolling? I've managed to achieve this, but now the table headers are not aligned properly with the rows below. I've also created a helpful jsfid ...

Array vs Single Object - Comparing AngularJS / Javascript Data Structures (Basic)

My array is very basic [ { ticketId: 1, name: "John", }, { ticketId: 124, name: "Ads" } ] I have displayed the data in a dropdown menu <ul class="dropdown-menu"> <li ng-repeat="ticket in tickets"> <a h ...

Creating an HTML table with colspan and rowspan using ng-repeat from a deeply nested JSON dataset

I'm working on creating a table layout shown in the attached image: Composite Class Routine Check out my progress so far in this Plunker demo: https://plnkr.co/edit/4WiWKDIM2bNfnmRjFo91?p=preview The JSON data I'm using is as follows: $scope. ...

IntelliJ is not able to detect certain JS libraries

Currently, I am working on an NDV3 AngularJS graph demo using IntelliJ. To start off, I have created a standard HTML file named index.html and included all the necessary AngularJS and NDV3 libraries in a folder called bower_components located within the r ...

Transform HTML or formatted text from an iPhone 4 Notes backup SQLite file into plain text

I was looking to recover some missing notes from an iTunes backup of an iPhone 4 by accessing the notes.sqlite file. After querying the table that stores the note text: select zcontent from znotebody I found that the text is in HTML format. Is there a wa ...

Positioning Elements Absolutely in Relation to List Items

I am looking to style a list item by adding background-color, border, and other properties. Below is the CSS and HTML I have tried so far. Additionally, I want to use the div element to position more absolute elements in relation to the li > div. ul { ...

Searching for various object values within an array and then adding two properties together in JavaScript

I am working with an array of objects that require me to analyze two properties in order to calculate a value. let data = [ { NodeId: "9837f279", NodeName: "Node1", summary: { current: 50, limit: 75 ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Transfer sound data blob to server and store it as a file

Currently, I have 3 buttons on my webpage - one to start recording audio, one to stop the recording, and one to play the recorded audio. Using MediaRecorder makes it easy to capture audio as a blob data. However, I am facing issues when trying to upload th ...

"Efficiently Browsing Through Various Links with the Help of Greasemon

My goal is to open approximately 25 hyperlinks from a single page. All of these hyperlinks include the text Free Win. I am looking for a solution that will open each individual link in a new tab within the browser. I have written a Greasemonkey script, ...

Tips for eliminating the entire link from the footer section

Whenever a link is hovered over, a white box displaying the full URL appears at the bottom left of the browser. Is there a way to remove this feature from my website? I appreciate any assistance with this issue. Thank you! ...

Exploring the file attributes within nw.js

I'm in the process of developing a native application using nw.js. I have included the following code snippet: <input id="fileDialog" type="file" accept=".pdf,.epub" multiple/><a id="add" href="#">Add</a> Below is my JavaScript cod ...

Auto-populate AngularJS form using PHP array via JSON and $http

I came across a fantastic autocomplete feature in this plunker that I need to replicate using a PHP array with $http. When I tried adding an alert to check my array, it worked perfectly. However, I'm stuck on how to implement the filter in AngularJS. ...

Using JavaScript to enhance event handling for a `select` element in a progressive manner

I have an advanced <select> element in my HTML. It is structured like this: <ul> <li></li> <li></li> ... </ul> In the current setup, a click event handler is attached to each individual li element. If the ...

Unraveling the mystery of extracting special variables from HTML textarea input

How can I parse the input from a textarea using PHP or JS to replace special variables like {name} with an actual name instead of displaying {name} literally? I want users to be able to represent someone's name by entering {name} in their submission. ...

Checkbox selection limitation feature not functioning correctly

Having trouble with my checkbox question function - I want to limit the number of checkboxes that can be checked to 3, but it's still allowing more than that. I suspect the issue lies with latestcheck.checked = false; This is my typescript function: ...

There seems to be a complete absence of rendering in this particular vue

Upon initializing a fresh Vue project using Vue CLI 3 and removing all default views and components, I proceeded to add two new views. To my surprise, these views did not render when changing the URL; instead, I was met with a blank page and no error messa ...

The error message "THREE is not defined" indicates a problem

Hey there! I've been experimenting with running some three.js examples on my local machine. I ran into some security issues, so I set up a local server using Python. While the background image and text appear as expected, the animations are not workin ...