How to troubleshoot and resolve a sticky invisible border issue

I'm working with a table that has sticky <td>'s as headers. Unfortunately, the table's <tr> and <thead> elements cannot be made sticky.

The problem I'm facing is that when the header becomes sticky, the borders disappear and it looks like there is extra padding.

Answer №1

After conducting some research, I stumbled upon the solution:

 border-spacing: 0; *border-collapse: collapse;
and it did the trick.

table{
  border-spacing: 0; *border-collapse: collapse;
}
td{
border:3px solid gray;
padding:3px;
}

tbody td{
  background:lightgreen;
}
.sticky{
  background:maroon;
  color:white;
  position:sticky;
  top:0;
}
<table>
  <thead>
    <tr>
      <td class="sticky">This is sticky header</td>
      <td class="sticky">This is sticky header</td>
      <td class="sticky">This is sticky header</td>
      <td class="sticky">This is sticky header</td>
    </tr>
   <tbody>
    <tr><td>Some random text</td></tr>
    [additional rows of random text]
    <tr><td>Some random text</td></tr>
</table>

Answer №2

To resolve the issue, include border-collapse: separate; in the table. This will style your borders to align with the <td> elements and ensure that they are truly connected to the <td>, resulting in a collapsed appearance.

Additionally: don't forget to add border-spacing: 0; to the table to eliminate any padding for the table cells.

table{
  border-collapse: separate;
  border-spacing: 0;
}
td{
border:3px solid gray;
padding:3px;
}

tbody td{
  background:lightgreen;
}
.sticky{
  background:maroon;
  color:white;
  position:sticky;
  top:0;
}
<table>
  <thead>
    <tr>
      <td class="sticky">This is sticky header
      <td class="sticky">This is sticky header
      <td class="sticky">This is sticky header
      <td class="sticky">This is sticky header
   <tbody>
    <tr><td>Some random text
    <tr><td>Some random text
    ... <!-- additional rows of random text -->
</table>

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

Generate a table inside a bootbox dialog using the data retrieved from an ajax call

I'm currently working on an app that requires attendance to be reported for specific events. I retrieve a list of JSON objects using AJAX. My goal is to create a table within a bootbox dialog that generates new rows for each item in the result from m ...

Creating a stationary div in Internet Explorer 10 Mobile

Is there a way to create a fixed div that is compatible with IE10 Mobile on Windows Phone 8? Currently, I have been using the following code: <div style="position:fixed;width:100%;background-color:blue;"> test </div> This solution is only ...

Is there a way to replicate a DIV and its contents in ASP.NET using C#?

I am looking to create a form that can duplicate or clone itself when the CPF/CNPJ (Brazilian personal identification) onchange. Below is the current code I have: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" r ...

The fixed background-attachment feature does not function properly in Chrome when it is contained within a scrolling div

Essentially, if a background image is placed within a scrolling div, it will no longer remain fixed and will revert back to scrolling: CSS: <div class="wrapper"> <span></span> </div> HTML: html, body{ width: 100%; height: ...

Display the Ajax response in a designated div

I am facing an issue with my ajax code. It doesn't print any output, but when I use alert() to print the output, it shows up fine. $(".grp-ans").each(function(e){ $.ajax({ type: 'POST', url: 'show-answ ...

Challenges with placement of Div elements

Check out my jsFiddle example here: http://jsfiddle.net/pqCGd/. Within the jsFiddle, you will find a large messages div that contains multiple message divs. In this example, I have manually provided an example of a message div instead of using auto-genera ...

Progress bar carousel component based on advancement movements

Here is the mockup I am working with: https://i.sstatic.net/bIepQ.png So far, I have made good progress with this code: https://codesandbox.io/s/codepen-with-react-forked-16t6rv?file=/src/components/TrendingNFTs.js However, I am facing a couple of challe ...

How can I create a hover effect for multiple divs sharing the same id using CSS or JavaScript in the easiest way possible?

When hovering, I want the opacity to be set at "0.3"; when not hovering, it should be set at "1". Additionally, the color of h2 and h3 in each div should transition from white to red. I have searched extensively online and tried to sol ...

There is a collision of boxes occurring within the CSS

Help Needed: Overlapping Boxes in Responsive Design I am facing a problem with my CSS where the boxes on my website are overlapping when I shrink the browser window. The issue occurs as I resize the window, causing the boxes to overlap more and more. I ...

When converting text to html, replace words with hyperlinks. When multiple words can be linked, prioritize the longer

Struggling with a task at hand: My goal is to enhance a string that contains HTML by linking certain words to corresponding "read more" pages. The challenge arises when these search terms overlap, resulting in nested links or terms not being replaced prop ...

How can you apply a class to a different element by hovering over one element?

Is there a way to darken the rest of the page when a user hovers over the menu bar on my website? I've been playing around with jQuery but can't seem to get it right. Any suggestions? I'm looking to add a class '.darken' to #conte ...

How come the Hover effect is activated even without hovering the mouse over the text?

I have designed a dropdown menu, but I am facing an issue with the portfolio section. When I hover over the portfolio, the dropdown menu appears correctly. However, when I hover over the text of the first dropdown menu, all sub-dropdown menus from other dr ...

I am having difficulty aligning text horizontally within a <div> element using a combination of <div>, <a>, and <p> tags

I can't seem to figure out why I'm struggling with this. Check out the screenshot below: Here is my current CSS: #tabbase { display : inline-block; width : 230px; height : 173px; border : 1px solid #3e3e3e; background-color : #1b ...

Mobile viewing causing problems with website buttons functionality

While my website functions perfectly on desktop, I am facing an issue where the buttons are not working when accessed through mobile devices. Interestingly, these buttons were operating fine in a previous version of the site. Although I have made minimal ...

Is there a way to have text positioned to the left and the right on a single line simultaneously?

Is there a way to format text so that some aligns left and some aligns right on the same line? <p>This text should be aligned left. This text should be aligned right.</p> I can align all text to the left or right, either inline or through a s ...

What could be causing my JavaScript code to malfunction, even though it appears to be coded correctly?

// JavaScript Document "use strict"; $(window).scroll(function(){ if($(window).scroll() > 100){ $("#scrollTop").fadeIn(); } }); $(window).scroll(function(){ if($(window).scroll() < 100){ $("#scrollTop").fadeOut(); } }); $(document).ready(function() ...

How to display an image in a pop-up with a title

I am currently using the Bootstrap framework and I am trying to create a popup image with a title, but it only shows the image. I am not sure how to add code in JavaScript to display the title as well. Can someone please assist me? Here is my code: Javas ...

Capture a section of the body background to incorporate into the canvas space

As a newcomer to the world of canvas, I have been learning from various sources about how it works. However, my goal is more dynamic and unique than what I've seen so far. I am looking to create a body background for my webpage that is responsive, cen ...

Placing a dynamic loading image in the center of a webpage

Positioning an ajax loading image in the center of a mobile page. The code snippet above is used to load database records via ajax by displaying a loading image in a Div element. My inquiry is regarding how to ensure that this loading image appears at th ...

Use .empty() method to remove all contents from the tbody element after creating a table

Currently, I am working on a project where I am creating a drop-down list to assist users in selecting media and ink for their printers. The goal is to then generate a table displaying the selected results. While I have managed to successfully generate the ...