What causes the disappearance of the right border in the initial fixed column while scrolling horizontally on my table?

After implementing sticky positioning for the first column in my table, I noticed that when scrolling horizontally, the right border of the first column disappears. How can I fix this issue? Below is the CSS and HTML setup I used:

th:first-child,
td:first-child {
    position: -webkit-sticky; /* for Safari */
    position: sticky;
    z-index: 1;
    left: 0;
    background: #fff;
}

Here's a snippet of the HTML and CSS code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            box-sizing: border-box;
        }

        .table-container {
            overflow: auto;
            border: solid;
            margin: auto;
        }

        table {
            width: 100%;
            table-layout: fixed;
            border-collapse: collapse;
        }

        th,
        td {
            width: 20%;
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th:first-child,
        td:first-child {
            position: -webkit-sticky; /* for Safari */
            position: sticky;
            z-index: 1;
            left: 0;
            background: #fff;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                    <th>Column 3</th>
                    <th>Column 4</th>
                    <th>Column 5</th>
                    <th>Column 6</th>
                    <th>Column 7</th>
                    <th>Column 8</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Data 1</td>
                    <td>Data 2</td>
                    <td>Data 3</td>
                    <td>Data 4</td>
                    <td>Data 5</td>
                    <td>Data 6</td>
                    <td>Data 7</td>
                    <td>Data 8</td>
                </tr>
                <!-- More rows as needed -->
            </tbody>
        </table>
    </div>
</body>
</html>

If you have any suggestions on how to keep the right border of the first sticky column visible while horizontally scrolling through the table, please let me know!

Answer №1

If you want to add a special effect using CSS, consider using pseudo elements with the following code:

This should do the trick nicely

th:first-child::after,
td:first-child::after {
  content: " ";
  background: #ddd;
  width: 1px;
  height: 100%;
  position: absolute;
  right: 0;
  top: -8px;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            box-sizing: border-box;
        }
    
        .table-container {
            overflow: auto;
            border: solid;
            margin: auto;
        }

        table {
            width: 100%;
            min-width: 900px;            
            table-layout: fixed;
            border-collapse: collapse;
        }

        th,
        td {
            width: 20%;
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th:first-child,
        td:first-child {
            position: -webkit-sticky; /* for Safari */
            position: sticky;
            left: 0;
            z-index: 1;
            background: #fff;
            border-right: unset;            
        }
        th:first-child::after,
        td:first-child::after {
            content: " ";
            background: #ddd;
            width: 1px;
            height: 100%;
            position: absolute;
            right: 0;
            top: -8px;
        }
    </style>\
</head>
<body>
    <div class="table-container"><table><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th><th>Column 4</th><th>Column 5</th><th>Column 6</th><th>Column 7</th><th>Column 8</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td><td>Data 4</td><td>Data 5</td><td>Data 6</td><td>Data 7</td><td>Data 8</td></tr><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td><td>Data 4</td><td>Data 5</td><td>Data 6</td><td>Data 7</td><td>Data 8</td><...>

Answer №2

To make the necessary adjustment, simply switch border-collapse: collapse; to border-collapse: separate;.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0>
    <style>
        * {
            box-sizing: border-box;
        }

        .table-container {
            overflow: auto;
            border: solid;
            margin: auto;
        }

        table {
            width: 200%;
            table-layout: fixed;
            border-collapse: separate;
        }

        th,
        td {
            width: 20%;
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th:first-child,
        td:first-child {
            position: -webkit-sticky; /* for Safari */
            position: sticky;
            z-index: 1;
            left: 0;
            background: #fff;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                    <th>Column 3</th>
                    <th>Column 4</th>
                    <th>Column 5</th>
                    <th>Column 6</th>
                    <th>Column 7</th>
                    <th>Column 8</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Data 1</td>
                    <td>Data 2&l/td>
                    <td>Data 3&l/td>
                    <td>Data 4</td>
                    <td>Data 5</td>
                    <td>Data 6</td>
                    <td>Data 7</td>
                    <td>Data 8</td>
                </tr>
                <!-- More rows as needed -->
            </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

obtaining an HTML element within a blank div using either jQuery or Knockout.js

I need help with a functionality involving 3 buttons. Each button, when clicked, should add a specific container to an empty div located outside of the buttons. <button id="a" data-bind="clickme" value="1">a</button> <div id="empty1"> & ...

Creating an Angular Material card that maximizes vertical space and allows its content to scroll effortlessly

My challenge is creating a material card (mat-card) that occupies all available space on the screen, with scrollable content inside (mat-card-content) because the items within exceed the available space. I attempted to address this issue using the CSS cod ...

Is it possible to convert nested CSS into Material-UI's CSS in JS method?

Here is the code for embedding an iframe: <style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: abso ...

Implement custom styles using media queries within ngView to ensure compatibility with IE browsers even after a page

Experiencing issues with CSS rules in IE10 when included in a page via ngView (using Angular 1.0.6). Works fine in other browsers (potentially IE11, not personally confirmed): Here is the CSS code: .checker { display: none; } @media (min-width: 1041 ...

Angular Bootstrap Datepicker provides us with a date object, but I need it in the Date format

My desired date format is "Wed Aug 07 2019 16:42:07 GMT+0530 (India Standard Time)", but instead I am receiving { year: 1789, month: 7, day: 14 } from ngbDatepicker. Any assistance on resolving this issue would be greatly appreciated. ...

Ways to position an element at the center using Flexbox技

Having some difficulty centering an element in my code using flexbox. It seems to work for everything else, but not here. Any suggestions or ideas? .answer { text-shadow: 0 0 2px gr; display: flex; justify-content: center; border-radius: 5px; ...

Enhancing an image with zoom effect in a 2-column layout on hover

Could someone help me achieve a 2-column layout where the left column contains text and the right column an image? I want the image in the right column to zoom when the user hovers over either the left or right column. The issue is that when the user hove ...

Preventing a keyboard from appearing when buttons are pressed

Ever created a tool that inserts text into a <textarea> using button inputs? You can check out mine here But here's the issue - on mobile devices, when users tap those buttons, the keyboard covers half of the screen. Any solutions to prevent th ...

Stop browser from downloading attached file

<a href="/scripts/script.pde" target="_blank"></a> My goal is to have the browser show this file as text in a new window. Once clicked, it will be downloaded onto the user's computer. ...

What is the reason this switch statement functions only with one case?

Why is the switch statement not functioning properly? It seems to correctly identify the values and match them with the appropriate case, but it only works when there is a single case remaining. If there are multiple cases to choose from, nothing happens. ...

Is there a way to append the current path to a link that leads to another URL?

I am currently in the process of separating a website, with the English version being on the subdomain en. and the French version residing on the www. Before making this change, I have a drop-down menu that allows users to select their preferred language ...

Struggling with a JQuery function that fails to perform addition

I'm experiencing an issue with my code where the math doesn't add up when trying to sum values under the venting selection. Instead of adding up, it just displays as text. I have an event set up that should populate the values upon selecting valu ...

What steps can be taken to enhance this Menu code?

I have a function that generates my main and sub menus based on a page ID I provide. It ensures the correct menu item is highlighted using CSS. My question is: How can I optimize this code (and similar ones)? How can I reduce the lines of code and avoid r ...

Attempting to alter the animations depending on the color button that has been selected

Bootstrap radio buttons are being utilized. I want the animation to change when one of these buttons is selected. I am currently working on changing colors in JavaScript. I attempted to use a for each loop but encountered errors stating that it is not a ...

Is it true that event.stopPropagation does not function for pseudoelements?

I am facing an issue with event handling in my ul element. The ul has three li children elements, and the ul itself has a useCapture event handler for click. In the click event handler, I successfully stop the event using event.stopPropagation(), and every ...

Is there a way to make this anchor link appear as a button?

Can someone please help me figure out how to make this element look like a button? I've tried using webkit but it doesn't seem to be working properly. I'm using HTML5, CSS3, and bootstrap. Your assistance would be greatly appreciated! https ...

Ways to eliminate spacing around lists?

I am trying to remove the margin on the left side of a list that contains 3 items. Here is my code: ul li { display: inline; list-style: none; } <ul> <li>I have margin space on my left side</li> <li>List 2</li> & ...

Transforming a numerical function into an HTML span tag: A quick guide

Looking for assistance with updating the last line. The goal is to display the current month, obtained from the getMonthText variable, within the <h1><span id="month_year">&nbsp;</span></h1> Starting code. The issue arises to ...

Why does the MySQL insert table only work once and then stop working?

Hey there! I need some help from the coding experts. I've hit a roadblock while trying to implement an add site function for my directory. Initially, everything was running smoothly - query created, program executed, column added to the table without ...

What are the Best Locations for Developing a Hybrid App?

My goal is to develop a Hybrid Application using HTML5, CSS3, JavaScript, jQuery Mobile, SQL, and AJAX. This is my first time attempting this. Can you recommend a platform for me to start building on? ...