Ensure that all `thead th` elements remain in a fixed/sticky position when using a data

I am facing an issue with my data table requirement. I need to have 2 thead elements in the table, both of which should stick when scrolling. However, only the last thead's tr stays stuck at the top while the first one gets hidden.

Can anyone help me resolve this issue? The problem seems to be the mixing of top positions of both elements, and since their heights are dynamic, I am unsure how to fix it.

CSS :

.container{
  border: 1px solid red;
  height: 100px;
  overflow-y: auto;
}

table{
  width: 100%;;
  border-collapse: collapse;
  position: relative;
}

thead{
  overflow-y: auto;
}

thead tr th{
  background-color: yellow;
  position: sticky;
  left: 0; top: 0;
}

Check out the live demo here

Thank you in advance for your assistance.

Answer №1

You have the option to achieve this with just a single thead element. However, if you are set on using two thead elements, it's important to assign different classes to each one. The second thead should also have a suitable value for the top property to ensure proper alignment.

In the following example, there are two thead elements. The first contains span and input elements, while the second one only has input elements like your example. Both sections stick to the top correctly.

.container{
      border: 1px solid red;
      height: 200px;
      overflow-y: auto;
    }

    table{
      width: 100%;;
      border-collapse: collapse;
      position: relative;
    }

    thead{
      text-align:left;
      overflow-y: auto;
      position: sticky;
      left: 0; top: 0;
    }
   .secondth{
      text-align:left;
      overflow-y: auto;
      position: sticky;
      left: 0; 
      top: 42px;
    }
    thead tr th{
      background-color: yellow;
    }
    thead tr th span{
    display:block;
    }
<!DOCTYPE html>

    <html>
      <head>
        <link rel="stylesheet" href="lib/style.css" />
        <script src="lib/script.js"></script>
      </head>

      <body>
        <div class="container">
          <table>
            <thead>
              <tr>
                <th>
                  <span>One</span>
                  <input type="text" value="search" />
                </th>
                <th>
                  <span>Two</span>
                  <input type="text" value="search" />
                </th>
                <th>
                  <span>Thr</span>
                  <input type="text" value="search" />
                </th>
              </tr>
            </thead>
            <thead class="secondth">
          <tr>
            <th><input type="text" value="search" /></th>
            <th><input type="text" value="search" /></th>
            <th><input type="text" value="search" /></th>
          </tr>
        </thead>
            <tbody>
              <tr>
                <td>comment1</td>
                <td>comment1</td>
                <td>comment1</td>
              </tr>
              <!-- Additional table rows -->
            </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

How to Build a Chatbox using AJAX, HTML, and C#?

Currently, I am incorporating the Nancy Web Framework into my C# Console Application to develop a user-friendly Web Administration panel for my software. In this project, I have chosen to utilize the Spark View Engine due to its simplicity in handling HTML ...

Navigation Menu Spanning Across Full Width of All Screens

Hello! I am currently working on designing a responsive menu using HTML5 and CSS. My approach involves creating a navigation menu for desktop computers and implementing a checkbox with a label to reveal the responsive menu when needed. Here is the code s ...

Troubleshooting an Angular.js "Hello World" application that is malfunctioning

I've been trying to get a simple Hello World app working by following different tutorials, but it doesn't seem to be functioning correctly. I've double-checked my code and everything looks right to me. Could someone please assist me in troub ...

What is the best way to prevent certain bootstrap classes from being applied across all elements?

After testing my HTML and CSS code on my local computer, it looks perfect. However, when I upload it to a server, the appearance changes. You can see it in action here: . The issue seems to be related to some bootstrap classes being activated differently ...

How can one generate an HTML element using a DOM "element"?

When extracting an element from an HTML page, one can utilize a DOM method like .getElementById(). This method provides a JavaScript object containing a comprehensive list of the element's properties. An example of this can be seen on a MDN documentat ...

Personalize the pagination or other elements of Splide in a Svelte component to suit your needs

I am currently integrating Splide into my Svelte application and facing an issue with the pagination styling. The pagination is appearing on top of my images and is too transparent for my liking. I'm utilizing Svelte Splide to incorporate this library ...

I'm attempting to make the button hover color transparent, but unfortunately, it's not having any effect

Check out the code snippet below. I'm attempting to adjust the button hover color to transparent. The div class is "button_container" while the button class is "btn". I made changes to the color initially, but it doesn't seem to be working. Any a ...

Troubleshooting Problem with Website Responsiveness on iPhones Using Bootstrap 5

I am currently experiencing a challenge involving the responsiveness of a website I am developing, particularly when it comes to iPhones. The site utilizes Bootstrap 5 and displays correctly on Android devices and in Chrome Dev Tools. However, upon testing ...

Running a CSS keyframes animation can be achieved by removing the class associated with it

Is there a way to reverse the CSS animation when a class is removed? I'm trying to achieve this on my simple example here: https://codepen.io/MichaelRydl/pen/MWPvxex - How can I make the animation play in reverse when clicking the button that removes ...

Retrieving a boolean value (from a JSON file) to display as a checkbox using a script

Currently, I am utilizing a script to fetch data from a Google Sheet $.getJSON("https://spreadsheets.google.com/feeds/list/1nPL4wFITrwgz2_alxLnO9VBhJQ7QHuif9nFXurgdSUk/1/public/values?alt=json", function(data) { var sheetData = data.feed.entry; va ...

Allow iframe to be edited within jsfiddle

I have a question that I need to ask soon, but first I need an editable iframe in jsfiddle. It's working fine on my local machine, but not on jsfiddle. I believe it's because of the frames being used? On my local machine, I use: setTimeout(&apo ...

Attempting to transfer a JSON object from a frontend Form Input to an Express backend

Apologies for bringing up what may seem like a basic issue, but I've been searching extensively (even through axios' documentation) and haven't been able to pinpoint exactly what I need to resolve this issue. I have created a simple To-Do we ...

having difficulty grasping the variable's scope within this code

Here we have a code snippet where the variable vid is initialized inside a function. The question arises on how this variable can be used outside the function, specifically in the context of vid.play(). How does the program know that vid was created usin ...

If the condition is based on the category in category.php and single.php

I am attempting to create a conditional statement that will display specific content only if the user is in a particular category or on a single.php page associated with that category. I have experimented with these codes, but unfortunately, neither of the ...

Issue with Vuetify carousel not resizing to fit the entire width and height

I am looking to create a carousel that spans the entire width and height of a viewport in my Vue.js 2 project using Vuetify. Below is the code I have so far: <head> <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500, ...

Steps to replace the content of an HTML file (such as modifying images) by clicking on an element in a separate HTML file

Currently, I am in the midst of a project and wondering if it is possible to dynamically modify the content of an HTML file, such as images and text, using JavaScript. My goal is to achieve this without relying on any frameworks, simply by clicking on el ...

Styling anchors that are focused or visited while scrolling using CSS and jQuery

I'm facing a challenging question that I can't seem to figure out on my own. At the top of my page, I have some anchors that smoothly scroll down to different articles: I would like to indicate to visitors their location on the page by rotating ...

Footer button overrides list components due to improper implementation of vertical ion-scroll

Having some trouble setting up ion-scroll on a specific screen in my mobile application built with Ionic. On the Book page of my app, I'm encountering two main issues: https://i.stack.imgur.com/MnheG.png 1) The placement of the Confirm button doesn& ...

Creating a stunning image reveal animation using GSAP and clip-path within a React environment

When attempting to create an animation for revealing an image using GSAP and the clip-path property within a Component, I encountered an issue. The animation works perfectly when using the first clip-path value, but as soon as I switch to the other one, th ...

Choose from a dropdown menu to either activate or deactivate a user account

I'm new to php and I need help getting this code to function properly delete.php <?php include_once 'dbconfig.php'; if($_POST['del_id']) { $id = $_POST['del_id']; $stmt=$db_con->prepare("UPDATE tbluse ...