The underlying background shade peeks through the sticky header

Is there a way to adjust the CSS so that the border color of the thead cells show over the background color of cells with unique columns? I have a table with sticky headers and rows containing unique column spans with background colors, and when scrolling, the background color overlaps the border color. Here is the relevant CSS:

table {
  border-collapse: collapse;
  position: relative;
}

table, th, td {
  border: 2px solid black;
}

th, td {
  padding: .5rem 1rem;
}

td[colspan] {
  text-align: center;
  background-color: red;
  color: white;
}

table thead:first-child th {
  position: sticky;
  top: 0;
  background-color: white;
}
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>row1 col 1</td>
      <td>row1 col 2</td>
      <td>row1 col 3</td>
    </tr>
    <tr>
      <td colspan="3">row2 col 1</td>
    </tr>
    <tr>
      <td>row3 col 1</td>
      <td>row3 col 2</td>
      <td>row3 col 3</td>
    </tr>
    <tr>
      <td>row4 col 1</td>
      <td>row4 col 2</td>
      <td>row4 col 3</td>
    </tr>
    <tr>
      <td>row5 col 1</td>
      <td>row5 col 2</td>
      <td>row5 col 3</td>
    </tr>
    <tr>
      <td>row6 col 1</td>
      <td>row6 col 2</td>
      <td>row6 col 3</td>
    </tr>
    <tr>
      <td>row7 col 1</td>
      <td>row7 col 2</td>
      <td>row7 col 3</td>
    </tr>
    <tr>
      <td>row8 col 1</td>
      <td>row8 col 2</td>
      <td>row8 col 3</td>
    </tr>
    <tr>
      <td>row9 col 1</td>
      <td>row9 col 2</td>
      <td>row9 col 3</td>
    </tr>
    <tr>
      <td>row10 col 1</td>
      <td>row10 col 2</td>
      <td>row10 col 3</td>
    </tr>
    <tr>
      <td>row11 col 1</td>
      <td>row11 col 2</td>
      <td>row11 col 3</td>
    </tr>
    <tr>
      <td>row12 col 1</td>
      <td>row12 col 2</td>
      <td>row12 col 3</td>
    </tr>
    <tr>
      <td>row13 col 1</td>
      <td>row13 col 2</td>
      <td>row13 col 3</td>
    </tr>
    <tr>
      <td>row14 col 1</td>
      <td>row14 col 2</td>
      <td>row14 col 3</td>
    </tr>
    <tr>
      <td>row15 col 1</td>
      <td>row15 col 2</td>
      <td>row15 col 3</td>
    </tr>
  </tbody>
</table>

Answer №1

Building on an idea mentioned in a comment on the topic of Border style do not work with sticky position element, a modification has been made to adjust the width specified for the borders, cutting them in half so that when combined they result in the necessary 2px thickness.

table {
  border-collapse: separate;
  border-spacing: 0;
  position: relative;
}

table,
th,
td {
  border: 1px solid black;
}

th,
td {
  padding: .5rem 1rem;
}

td[colspan] {
  text-align: center;
  background-color: red;
  color: white;
}

table thead:first-child th {
  position: sticky;
  top: 0;
  background-color: white;
}
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>row1 col 1</td>
      <td>row1 col 2</td>
      <td>row1 col 3</td>
    </tr>
    <tr>
      <td colspan="3">row2 col 1</td>
    </tr>
    
    <!-- remaining table rows truncated for brevity -->
    
  </tbody>
</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

Express causing textarea in http form to have empty req.body

Here is a form for users to upload a file and submit text: form(action='/createpost' enctype="multipart/form-data" method='post' id="imgForm") input(type='file' name='imgPath' size = "60") br textarea(na ...

Properly nested anchor elements with semantic meaning

I'm currently working on a web application where we are implementing inline editing. To illustrate the issue I'm trying to address, let's use the example of a Facebook post. For instance: Jennifer Lopez shared a new video The name "Jennif ...

Insert more text following the existing content

Is it feasible to include text using pseudo-elements ::after or ::before to a specific word? For example, I am interested in placing a PDF icon consistently beside the word "Download". [PDF] Download Alternatively, are there other methods available? ...

HTML5 Device Orientation Emulator

Is there a tool to simulate device orientation for an HTML 5 interpreter (also known as a web browser)? I have been experimenting with the DeviceOrientation Event Specification in HTML 5 and I have successfully coded an example that works on my Android sm ...

Steps for transferring PHP variables to a Bootstrap 4 modal

I am working on a Laravel project where I have implemented recursion to create different HTML elements for each item. My goal is to open a modal when clicking on these links and pass the appropriate variable based on the clicked element. EDIT: Changes mad ...

Exclusive carousel design for mobile users with bootstrap framework only

I need help with customizing the bootstrap4 carousel. Currently, I have a carousel that works on all devices, but I want it to only display on mobile devices. On desktops, I want the photos to be displayed side by side. I know I can create two container ...

What is the best way to limit the number of visitors allowed on a webpage?

I'm in the process of working on an application that includes an edit button: Here's my edit button: <th title="Edit task" class="edit" style="float: right; $color;"> <?php echo "<a href=edit.php?id=" . $row["id"] . "> ...

What are the steps to modify a div for kids?

Currently, I am working on an HTML code where the number of components that need to be edited varies, it could range from 3 to 20. To illustrate my situation, I have provided a small example on my website. You can see that my script successfully modifies ...

Utilize Ajax to open and close an HTML tag within separate div elements

I am facing a challenge in my project where I have to dynamically open and close a form using ajax. The form needs to be opened within a div, and then closed in another div below it like the following: Opening form (header div) $('#header').h ...

Addressing some minor visual inconsistencies in an HTML bullet-pointed list

After reviewing my current code, it is displaying the following: https://i.stack.imgur.com/dvjrc.jpg I am attempting to make the following changes: Ensuring each line in the list fits properly within its corresponding box by utilizing vertical-align: to ...

Div with headers that stick to the top and stack when scrolling

I am working on creating a lengthy scrollable list of grouped items where the group titles remain visible at all times (stacked). When a user clicks on a group header, the page should scroll to the corresponding items. I have successfully used the positio ...

Enhance user experience by incorporating an auto-complete feature into the date

Here's my current challenge: I have two date picker inputs and a button: When I select a date from the first input (let's say 11/19/2013), I want the second input to automatically show the next day's date (in this case, 11/20/2013) as soon ...

Is it possible to center content with Bootstrap?

Is there a different method to center content with empty space on either side? For instance: <div class="col-md-3"></div> <div class="col-md-6"> <div class="form-group"> .... </div> </div> <div class="col ...

Create a dynamic pulse line on an HTML5 Canvas

Hello everyone, I've been attempting to grasp HTML5 Canvas animation without much success. I was hoping to create the shape below by animating a custom shape every 10 seconds interval. Unfortunately, I got lost in the math and ended up manually writi ...

Scaling problem faced on iOS devices with HTML5 canvas

I've been working on a project using phonegap, trying to develop it for both Android and iOS. The main goal is to select a saved image from the phone library, display that image on an HTML img element, and then draw that img onto a canvas. Users shoul ...

What is the process for extracting the background color from a typescript file in Angular and connecting it to my HTML document?

My typescript array population is not changing the background color of my div based on the values in the array. I've attempted to set the background using [style.backgroundColor]="statusColor[i]", where statusColor is an array declared in my typescrip ...

Create a full-width slider using Materialize CSS framework

When using materializecss to create a slider, I encountered an issue where the image is full width but not full height, causing scrollbars to appear. What changes can I make to ensure the slider fills out my screen without any scrollbars? Additionally, I ...

Reduce the flexbox container to match the dimensions of its child elements

I have a set of blocks with fixed dimensions that I want to arrange in a grid layout. Using flex-wrap: wrap, I can distribute as many blocks as possible in each row. However, I need the entire wrapped column to be centered on the page, similar to this: ht ...

Executing functions after the completion of a CSS animation

I am currently utilizing Vue3. I have implemented a feature where the box grows in size when the mouse hovers over it. .curl:hover { width: 200px; height: 200px; } I am looking for a way to notify the user once the animation is complete and the size has ...

My attempt to reveal hidden information in my repeater using a button and JavaScript has not produced the desired results

Welcome to my first post on this forum. In the HTML code, there is a repeater containing a button shown below: <button type="button" class="btn btn-info">Contact</button> <p style="display: none" id="contact"><%# Eval("Email") %&g ...