When scrolling, the sticky <td> element on the table is overlapping with the sticky <th> header, causing the <td> border to disappear. What is the solution to fix this issue?

I am trying to set up a table that has a fixed header when scrolling up and a fixed first column when scrolling left. I have applied 'position: sticky' with 'top:0' for the header and 'left:0' for the first column. However, I am facing issues where the header text gets covered on scroll-up and the border of the sticky column disappears on scroll left. Can someone assist me in resolving this problem?

<!DOCTYPE html>
<html>
<head>
  <style>
    /* CUSTOM */
    .tableFixHead {
      overflow-y: auto;
      max-height: 300px;
      margin-left: -15px;
      margin-right: -15px;
      margin-bottom: 100px;
    }
    .tableFixHead thead th { position: sticky; top: 0; }
    /* Borders */
    .tableFixHead,
    .tableFixHead td {
      box-shadow: inset 1px -1px #293033;
    }
    .tableFixHead th {
      box-shadow: inset 1px 1px #293033, 0 1px #293033;
    }

    table {
      border-spacing: 0;
    }

    th {
      padding: 8px;
      color: #e9ecef;
      background-color: #1e2324;
      font-weight: 500;
      font-size: 14px;
    }
    thead{
      text-align: center;
    }
    td {
      background-color: #041230;
      text-align: center;
      padding: 10px;
      color: #e9ecef;
      min-width: 150px;
      font-size: 13px;
      word-spacing: 2px;
    }

    td:nth-child(1) {
      min-width: 50px;
      max-width: 50px;
      font-weight: bold;
      color: #66FCF1;
      position: sticky;
      left: 0;
      border: 1px solid red;
    }
    .fix-table
    {
     /* margin-top: 100px;*/
    }
  </style>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="resCSS.css">



</head>

<body>

  <div class="container">

    <div class="tableFixHead">
      <table class="tablecolor2">
        <thead>
          <tr>

            // Remaining HTML content would go here

          </tr>
        </thead>
        <tbody>
          
            // Remaining HTML content would go here
          
        </tbody>
      </table>
    </div>

    </div>

</body>
</html>

         

Answer №1

Make use of the z-index property for the table header elements and consider using a background to replace the border for the left elements

Take a look at the comments provided within the code snippet below:

.tableFixHead {
  overflow-y: auto;
  max-height: 300px;
  margin-left: -15px;
  margin-right: -15px;
  margin-bottom: 100px;
}

.tableFixHead thead th {
  position: sticky;
  top: 0;
  z-index:1; /* included */
}

/* Custom Borders */
.tableFixHead,
.tableFixHead td {
  box-shadow: inset 1px -1px #293033;
}

.tableFixHead th {
  box-shadow: inset 1px 1px #293033, 0 1px #293033;
}

table {
  border-spacing: 0;
}

th {
  padding: 8px;
  color: #e9ecef;
  background-color: #1e2324;
  font-weight: 500;
  font-size: 14px;
}

thead {
  text-align: center;
}

td {
  background-color: #041230;
  text-align: center;
  padding: 10px;
  color: #e9ecef;
  min-width: 150px;
  font-size: 13px;
  word-spacing: 2px;
}

td:nth-child(1) {
  min-width: 50px;
  max-width: 50px;
  font-weight: bold;
  color: #66FCF1;
  /* updated */
  background:
    linear-gradient(#041230,#041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat,
    red;
  /**/
  position: sticky;
  left: 0;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

  <div class="container">

    <div class="tableFixHead">
      <table class="tablecolor2">
        <thead>
          <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
            <th>Header 4</th>
            <th>Header 5</th>
            <th>Header 6</th>
            <th>Header 7</th>
            <th>Header 8</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <!-- Additional Rows Go Here -->
        </tbody>
      </table>
    </div>
    <!-- End of Table Snippet -->


  </div>
  <!--  End of container -->

</body>

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 objects be positioned or moved in a linear fashion, and how to incorporate free fall when adding additional objects?

I'm interested in creating a system similar to a "vibrating table" with linear displacement. After studying various tutorials, I was able to achieve simulations based on the following examples: Following the tutorial "Authoring a Multibody Simulatio ...

Anchor link leading to an incorrect location

Sorry for the potentially silly question, but I'm struggling to figure out what's happening here. I'm currently working on a website and trying to create an anchor link at . I've placed the anchor just before in this code: <a name ...

Ways to showcase HTML table in a four-column layout/grid

Delving into Dynamic HTML table creation, I'm currently using jquery for rendering purposes. At the moment, I am only displaying the table. The Goal I aim to segment my table into four columns or a grid structure Something akin to this: https://i. ...

The Alchemy feature on hover is not functioning

I am currently using alchemy.js to display a graph, but I am encountering issues with showing the "onMouseOver" caption of the graph's node. The console is displaying some errors which you can see here. Here is the code snippet: <html> < ...

How to integrate a toggle switch into an Angular datepicker component

Can the toggle switch be placed inside the calendar? I would like to have it positioned at the top of the open calendar so that users can choose to view date details using the toggle switch <form #uploadForm="ngForm" (keydown.enter)="$event.preventDe ...

The iFrame that is generated dynamically becomes null when accessed from a page that has been loaded using JQuery

One issue I am facing is with a dynamically created iframe in regular javascript. It functions perfectly fine when called from a static page using conventional methods. However, when it is being called from a page loaded by jQuery, I encounter an error s ...

How to automatically open JQuery Accordion panels when the page loads

My Accordion loads with the 1st panel open upon page load, but I am looking for a way to have the entire Accordion closed by default. Any help or suggestions on how to achieve this would be highly appreciated. Thank you for your assistance. FIDDLE http:// ...

Manipulate Input Classes by Adding and Removing Them

I am currently using masked JS in some input fields, specifically for telephone numbers. However, I am facing an issue where phone numbers can vary in length, either 10 or 11 digits. I need to switch the class based on whether a checkbox is checked. Here i ...

Ways to retrieve various data elements from ajax using PHP

Can you help me with my AJAX code that retrieves data from a PHP script? I want to know how to set multiple echo variables in PHP and display them separately using AJAX without reloading the page. AJAX Code $(document).ready(function() { //el ...

Struggling to integrate the navigation bar with Wordpress platform

For a while now, I have been facing challenges with the navigation bar on the WordPress theme I am currently creating. I am struggling to get the navigation bar to display correctly. Below is the code snippet from my header.php file: <body> &l ...

Injecting JavaScript object values into dynamically generated modal

I have a JavaScript object that contains various training courses. Each category includes multiple training courses, and each course is associated with a specific category. Currently, I am able to display the title and description of each category in a mo ...

"Defining a CSS class specifically for styling the span element nested within an

The HTML structure I am working with is as follows: <p> <a href="#"> <span class = "class-for-span"> SOME TEXT HERE </span> </a> </p> Along with the following CSS: p a{ color: grey; text-decora ...

Designing Interactive Circular Dates on Website

Currently, I am working on a webpage using HTML, CSS, JavaScript, and PHP. The goal is to design a page that features 7 circles representing the current date and the next 6 days. Users should be able to click an arrow to navigate to the following 7 days. ...

What is the best way to apply a conditional class to multiple elements with jQuery?

Hey there, I'm new here so please forgive any mistakes I might make or if I break any etiquette rules! I'm trying to create some cool buttons for radio inputs on a form using field yields. The idea is to have them start off in grayscale and then ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

Problem with displaying Django input fieldsSolution for missing Django input

Currently, I am in the process of constructing a website using Django. One of my tasks involves transferring input from index.html to about.html through view.py. However, despite successfully passing the input as seen in the URL in the browser's addre ...

Utilizing Row and Column Layouts in Bootstrap for Ordering Angular Lists

Is it possible to use bootstrap and angular to display a list in columns like shown below? The first four entries should be placed in the first column before moving on to populate the second column. a e b f c g d h Below is an example cod ...

CSS documents are being displayed as type text/plain

My goal is to host my static blog (built with Jekyll) on my Ubuntu server, but I'm encountering a problem where the CSS isn't being applied and I keep seeing this error: "Resource interpreted as Stylesheet but transferred with MIME type text/pla ...

Converting CSS to SCSS using a for loop

I am currently learning SCSS and I have successfully converted the CSS code below to SCSS, but now I am struggling with completing the rest. I have tried looking for help online, but so far no luck. Can someone please assist me with this? .par-1 { wi ...

What is the best way to adjust a div's height to fill the remaining space of the viewport after the header's height

Is there a way to create a hero section that fills 100vh minus the height of the header, especially when the height of the header is not fixed? I initially used CSS with a height property set to calc(100vh - 310px), where 310px represents the height of t ...