What is the best way to ensure a table cell remains fixed to the top and left side of the table simultaneously?

My table with a sticky header is giving me trouble. I initially set the position property as position:sticky; top:0;, and then tried to overwrite it for the first column using position:sticky; **left:0**;. However, the heading of column 1 remains sticky only when the previously set background color is removed. Otherwise, the text flows behind it or gets overlapped by upcoming headings. Can someone please assist me in resolving this issue?

<!DOCTYPE html>
<html>
<head>
  <style>
    /* CUSTOM */
    .tableFixHead {
      overflow-y: auto;
      max-height: 300px;
      margin-left: -15px;
      margin-right: -15px;
      margin-bottom: 100px;
      /*min-height: 200px;
      max-height: 500px;*/
      /* min-width: 600px;*/
    }
    .tableFixHead thead th { position: sticky; top: 0; z-index: 1; /*use z-index to make heading sticky on scroll with sticky column*/ }
    /* 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;
      font-size: 14px;
    }
    th {
     /*
     ext-align: center;*/
      padding: 8px;
      /*padding-left: 20px;*/
      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;*/
      background: linear-gradient(#041230,#041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat,
    red;
    }
  th:nth-child(1) {
      color: #66FCF1;
      position: sticky !important;
      left: 0 !important;
      top:0 !important;
      border: 1px solid red;
     /* background: linear-gradient(#041230,#041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat,
    red;*/
    }
  </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>
            <th>#</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>
          <!-- Rows go here -->
        </tbody>
      </table>
    </div>
      <!-- Table Dormant / Discontinued -->
    </div><!--  container -->

</body>
</html>

         

https://i.sstatic.net/C3Cz5.pnghttps://i.sstatic.net/mNIw3.png

Answer №1

Make sure to set the z-index property only for that specific cell:

.tableFixHead thead th:nth-child(1) {
  z-index: 1;
}

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

.tableFixHead thead th {
  position: sticky;
  top: 0;
}


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

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

<!--More content and code in similar manner-->

      </tbody>
    </table>
  </div>
  <!-- Table Dormant / Discontinued -->
</div>
<!--  container -->

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

The Chrome browser on a PC is displaying the mobile version rather than the desktop version

I am currently working on a website project using bootstrap, and I ran into an unexpected issue where the mobile version of the site is being displayed in Google Chrome on my laptop instead of the desktop version. This happens even when Inspect Element is ...

What is the method for inserting a newline into a textarea with an HTTPRequest?

Is it possible to print a newline in a textarea using the HTTPRequest property? Here's the code I have: <script> function sendRequest(str) { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } el ...

Attempting to align my range picker horizontally in a single row

After selecting the date range option from my dropdown button, I noticed that the date range options are stacking on top of each other instead of appearing in the same row. Has anyone encountered this issue before? One solution I attempted was adjusting th ...

Loop through items in a list using Angular.js and display each item within an <

I am facing an issue where the model returned from the server contains html tags instead of plain text, such as b tag or i tag. When I use ng-repeat to create a list based on this model, the html is displayed as pure text. Is there a filter or directive av ...

I am attempting to design an HTML page to serve as the body of an email. Can anyone advise me on how to effectively pass it into my setBody() function while using yiiMail?

On one of my .php pages, I have the following code snippet: <html> <head></head> <body> Hi <?php echo $user['first_name'].' '.$user['last_name']?>, <br><br> To reset your ...

Style sheets for two dynamically-sized boxes side by side

There are two boxes or columns positioned on the same line. Both have varying widths that need to remain on the same row. To clarify: .----------container 570px-----------. |[box1] [box2]| Ideal scenario | ...

Using PHP to enhance Bootstrap grid functionality

Trying to create a custom theme for my Omeka site using PHP to pull in item components. Each item is enclosed in its own div, and I'm attempting to arrange them in a grid using Bootstrap. However, they are currently stacking vertically instead of hori ...

Transferring MySQL information to web pages using hyperlinks within a table

We have encountered a challenge while working on our game shop website, specifically related to the purchase link. The link is displayed within a mysql table and currently directs users to the same page. Our goal is to optimize efficiency by adding new gam ...

Reading custom CSV files in PHP, with separate echoes for specific rows and columns

I have a basic CSV file with 6 columns. EDIT: I'm having trouble finding the error in the code below, specifically with selecting a key and using it as a variable: $handle = fopen('hire_pa_amps.csv',"r"); while($values = fgetcsv($handle)) ...

Can you see the visible area tag?

Is there a way to make an HTML element visible at all times, not just on focus? It seems like it should be as easy as this: HTML: <img src="image.png" width="100" height="100" usemap="#Map" /> <map name="Map" id="Map"> <area shape="circl ...

Is there a way to transform an HTML canvas into a tangible photograph?

I am currently working on converting HTML content into a real image on the client's side. After conducting research on canvas2base64, canvas2base64, and html2base64 on Stack Overflow, I have found that the generated images are always based on base64 c ...

Prevent horizontal HTML scrolling while displaying a layer

I am currently working with a .layer div element that darkens the page to highlight a modal. However, I have encountered an issue where upon triggering the event, the div does not occupy 100% of the screen and the original browser scroll bar disappears. I ...

The form isn't accessible through a new tab

I'm having an issue with the code I've written. Even though I specified target="_blank" in the form, it's not opening in a new tab like I intended. <script> function navigate() { var webUrl = "https://website.com/search/" + document.g ...

The sticky top feature of the Bootstrap 4 navbar does not function properly when it is nested inside a div

I am just getting started as a beginner working on a Web application and utilizing Bootstrap 4 for my web design. I have a quick question that needs some clarification. When the code is structured like this, the Navbar does not stay fixed at the top: < ...

Is there a significant time-saving benefit to utilizing nested rules in CSS, particularly when using HAML or LESS?

Here is how I am currently coding my CSS: Regular CSS: #content { color: white; background: black; } #content a { color: yellow; } #content p { margin: 0; } #content (etc...) #contnet (etc...) #content (etc...) (I always include the parent eve ...

Implementing Bootstrap Tags Input for a fresh start!

Currently, I am using Bootstrap Tags Input. When I press "ENTER," it automatically converts the text into a tag. However, I would like to be able to combine free text with tags without the "ENTER" key converting the text. Is there a way to achieve this? & ...

When a media query is activated, the Flex item mysteriously vanishes from

UPDATE I followed the suggestion in Ezra Siton's answer and changed the media query display to block, which successfully resolved the issue! Within my html code, I have a parent column flexbox containing two children. The second child is itself anoth ...

Switch between two checkbox options by simply clicking on one without the need for JavaScript

Is there a way to select two combined checkboxes with one click? I have a small calendar where I need to mark weeks using checkboxes. Since one week can fall into two months, I have created duplicates of the same checkbox for each week. Is there a safe wa ...

Tips for utilizing an event listener for a submission button

I am encountering a problem with this event listener. I attempted to add an event listener to the submit button, but it seems to be inactive. I can't figure out what mistake I have made! Here is my code: const form = document.getElementById("form") ...

What is the formula for determining the grid width when an item exceeds the column size?

I am working with a grid that matches the size of an image, which can be adjusted to be both smaller and larger in size. https://i.sstatic.net/n8Fxg.png Within this grid, I have 6 items. https://i.sstatic.net/LGmUb.png Each item is wider than the colum ...