Tables with fixed columns are experiencing border problems when scrolling horizontally

Is there a way to prevent the border from disappearing when scrolling? How can I achieve that or start the scroll only from the scrolled part?

https://i.sstatic.net/YOsyo.gif

Exploring HTML Table Structures

  <div class="wrapper">
    <table>
      <thead>
        <th class='fix'>Fixed</th>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
        <th>Col 4</th>
        <th>Col 5</th>
        <th class='fix'>Fixed</th>
      </thead>
      <tbody>
        <tr>
          <td class='fix'>First Content</td>
          <td>A1</td>
          <td>A2 (with longer content)</td>
          <td>A3</td>
          <td>A4</td>
          <td>A5</td>
          <td class='fix'>Last Content</td>
        </tr>
      </tbody>
    </table>
  </div>

Optimizing CSS for Tables

.wrapper {
  overflow-x:scroll;
  width:100%; 
}
table {
  table-layout: fixed; 
  width: 100%;
  border-collapse: collapse;
  background: white;
}
thead {
  font-family: arial
}
tr {
  border-bottom: 1px solid #ccc;
}
td, th {
  vertical-align: top;
  text-align: left;
  width:100px;
  padding: 5px;
  border: 1px solid red;
}
.fix {
  position:sticky;
  background: white;
}
.fix:first-child {
  left:0;
  width:120px;
}
.fix:last-child {
  right:0;
  width:120px;
}

Try it out on JSBin: https://jsbin.com/marezen/1/edit?html,css,output

Answer №1

To achieve fixed table borders, follow these steps:

First, remove the border-collapse property from your CSS.

table {
  table-layout: fixed; 
  width: 100%;
  /*border-collapse: collapse;*/
  background: white;
}

Next, add cellspacing="0" to eliminate any cell spacing.

<table cellspacing="0">

In order to maintain consistent borders like in the example provided, combine the following solutions:

CSS-

.wrapper {
  overflow-x:scroll;
  width:100%; 
}

table {
  table-layout: fixed; 
  width: 100%;
  /* border-collapse: collapse; */
  background: white;
  border-top:1px solid red;
  /* border-left:1px solid red; */
}

thead {
  font-family: arial
}

tr {
  /* border-bottom: 1px solid #ccc; */
}

td, th {
  vertical-align: top;
  text-align: left;
  width:100px;
  padding: 5px;
  /* border: 1px solid red; */
  border-right:1px solid red;
  border-bottom:1px solid red;
}
.fix {
  position:sticky;
  background: white;
  border-left:1px solid red;
}
.fix:first-child {
  left:0;
  width:120px;
}
.fix:last-child {
  right:0;
  width:120px;
}

td:nth-child(6),
th:nth-child(6){
  border-right:none;
}

HTML-

<div class="wrapper">
    <table cellspacing="0">
      <thead>
        <th class='fix'>Fixed</th>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
        <th>Col 4</th>
        <th>Col 5</th>
        <th class='fix'>Fixed</th>
      </thead>
      <tbody>
        <tr>
          <td class='fix'>First Content</td>
          <td>A1</td>
          <td>A2 (with longer content)</td>
          <td>A3</td>
          <td>A4</td>
          <td>A5</td>
          <td class='fix'>Last Content</td>
        </tr>
        <tr>
          <td class='fix'>First Content (with longer content)</td>
          <td>B1</td>
          <td>B2</td>
          <td>B3</td>
          <td>B4</td>
          <td>B5</td>
          <td class='fix'>Last Content</td>
        </tr>
        <tr>
          <td class='fix'>First Content</td>
          <td>C1</td>
          <td>C2</td>
          <td>C3</td>
          <td>C4</td>
          <td>C5</td>
          <td class='fix'>Last Content (with longer content)</td>
        </tr>
      </tbody>
    </table>
  </div>

Answer №2

To solve the issue, I implemented the addition of border-left to the wrapper element. This allowed me to remove the border property from td, tr and apply specific borders like border-top to prevent overlap with the wrapper's border.

Visit this link for more information

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 solely the words found within HTML documents

In my Python 2.7 project, I have a folder containing multiple HTML pages that I need to extract only words from. My current process involves opening the HTML file, using the Beautiful Soup library to extract text, and then writing it to a new file. However ...

Is there a way to toggle the slide effect of one 'div' without affecting another one?

Hey everyone! I am new to jquery and currently working on a project with two div elements that are slidetoggled. The issue I am facing is that when I open or close one div, the other div also moves along with it. I understand what is happening in the back ...

Deciphering unknown data for tabling using AJAX and jQuery

I am currently utilizing an API to retrieve a response. Within the ajax success function, I receive a response structured like this: { "Capabilities": { "System": { "SystemLogging": "true", "SystemBackup": "true", ... "DHCPv6 ...

Limit the width of the div to match the width of its only child element

My goal is to incorporate image captions on my website, but I've run into an issue with compatibility. Since the figure tag isn't supported in old versions of IE such as IE8 or below, I'm considering using a <div> containing an <img ...

Utilize flexbox to achieve center left alignment of elements within a DIV container

I've encountered a puzzling issue with my inherited markup. The width is defined on the outermost div and then inherited by each child div, creating a dynamic width scenario. The challenge I face is aligning the pink wrapper div to match the exact wi ...

Is it possible to activate the CSS max-width @media query without manually adjusting the size of the browser window?

On my website, the <body> container has a max-width of 1000px and a width of 100%, allowing it to display properly on various resolutions ranging from 100px to 4000px (and even higher) - making it fully responsive. I utilize @media queries to adjust ...

Determine the video on a website with the highest number of clicks

I am looking for assistance with storing click counts for videos on my asp.net website in a database. The goal is to track the most viewed video on the site. Can anyone provide guidance on how to achieve this? Below is the HTML code: <div class="row"&g ...

The radio button is unable to be deselected within the radiogroup due to the presence of a

I have developed a customized radio button component and want to group them together. However, when I utilize a v-for loop to display multiple radio buttons, they do not deselect when another option is selected. This is the code template for my radio butt ...

Creating artistic masterpieces on a digital canvas with the use of touch

After conducting some research, I am struggling to find a solution that allows touch screen users to draw on the canvas tag using raw JavaScript. Despite my best efforts, I keep hitting a dead end and my canvas remains unresponsive. The canvas is located ...

"Exploring the process of implementing a fixed method POST in Angular 5

When developing an application for Portal, I encountered an issue where a newly created role is not displayed without refreshing the browser. How can I ensure that the added element is directly displayed in the table without needing to refresh the browser? ...

How can I achieve a transparent background for a radio button in Chrome?

Today, I spent some time experimenting with the radio button to see if I could make its background transparent. I attempted the following CSS styling: input[type=radio] { background-color:transparent; background-image:none; -webkit-backface-v ...

Animate html elements one by one with delays using jQuery or plain JavaScript

After extensive research, I am still struggling to find the perfect solution for my problem. Before suggesting the setTimeout() function, please hear me out. I have around 20 HTML elements that are currently invisible. What I want is for them to become vi ...

Tips for saving the visibility status of a <div> in your browser bookmarks?

Currently, I am in the process of developing a single webpage (index.html). The navigation bar at the top includes links to hash references (DIV IDs). If JavaScript is enabled, clicking on these links triggers a JavaScript function with the onclick attribu ...

Creating a JavaScript automated slideshow with multiple instances of loops and/or setInterval running simultaneously?

Currently, I am facing a challenge while trying to create a basic automated slideshow from scratch. Though I have successfully built manual slideshows in the past, automating them is proving to be more complex. I started by experimenting with a for loop st ...

Tips for enabling auto-scroll feature in MuiList

Currently, I am working on a chat window component that utilizes Material UI for styling. I expected that setting a height or max-height on either the MuiList or MuiBox encapsulating the list would automatically scroll to the new message when it's sen ...

Looking for assistance with arranging and managing several containers, buttons, and modals?

My goal is to create a grid of photos that, when hovered over, display a button that can be clicked to open a modal. I initially got one photo to work with this functionality, but as I added more photos and buttons, I encountered an issue where the first b ...

Diverse Values within the Inner Border

Is it possible to create an inner border with unique values for each side? For instance: Top: 20px Right: 80px Bottom: 40px Left: 10px Can you provide an example of how this can be achieved? Thank you! :) ...

What is the best way to remove the responsive features of a Bootstrap website when the width is less than 786

My current dilemma is with my Bootstrap-based website. I am trying to make it unresponsive for devices with a screen width of less than 768px, but responsive as usual for widths greater than or equal to 768px. This is what I have attempted so far. In my g ...

execute a different function once the animation has finished running with the .bind

When attempting to detect the completion of a CSS animation in order to remove a class, I encountered an issue where using .bind causes the animation end event to trigger even for the other function. You can view the complete code here. How can I ensure ...

"Exploring the functionality of jQuery click events within the Bootstrap Button plugin

Is there a way to determine which radio button is selected when clicking on a radio button using the Bootstrap Button plugin? The following code snippet always evaluates to false, regardless of which radio button is clicked: HTML <div class="btn- ...