What can be done to stop a header from sticking to a table and displaying horizontally scrolling content along the border?

I need to ensure that the red headers do not overlap with the blue headers' borders while scrolling horizontally. It is essential for me to have a white border on the blue headers.

.container {
  overflow: auto;
  width: 200px;
}

table {
  border-collapse: collapse;
}

th {
  background-color: red;
  border-right: 1px solid white;
  color: white;
  padding: 10px;
  z-index: 1;
}

.sticky {
  background-color: blue;
  position: sticky;
  z-index: 2;
}

.test1 {
  left: 0;
}

.test2 {
  left: 57px;
}

.test3 {
  left: 114px;
}
<div class="container">
  <table>
    <thead>
      <tr>
        <th class="sticky test1">
        Test1
        </th>
        <th class="sticky test2">
        Test2
        </th>
        <th class="sticky test3">
        Test3
        </th>
        <th>
        Test4
        </th>
        <th>
        Test5
        </th>
        <th>
        Test6
        </th>
      </tr>
    </thead>
  </table>
</div>

I need help in ensuring that the red headers do not intrude into the border of the blue headers when scrolling horizontally. The presentation of having a white border around the blue headers is crucial for me.

Answer №1

If you want to achieve a border effect without actually using borders, one trick is to remove all borders and utilize CSS pseudo-elements on each header cell.

Furthermore, it's recommended to set fixed widths for your sticky columns so that adjusting the left property becomes simpler. I had encountered slight shifts in alignment (~1px) while scrolling horizontally until I used the exact width for my sticky columns.

.container {
    overflow-x: auto;
    width: 200px;
}

table {
    border-collapse: collapse;
}

th {
    background-color: red;
    color: white;
    padding: 10px;
    position: relative;
}

.sticky {
    background-color: blue;
    position: sticky;
    z-index: 1;
}

th::before {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 2px;
    /* Adjust fake border width here */
    height: 100%;
    background-color: white;
}

.test1 {
    left: 0;
}

.test2 {
    left: 55.86px;
    /* this is the exact width of the column header */
}

.test3 {
    left: calc(55.86px * 2);
    /* made this exact to stop the tiny shifts when scrolling */
}
<div class="container">
    <table>
        <thead>
            <tr>
                <th class="sticky test1">
                    Test1
                </th>
                <th class="sticky test2">
                    Test2
                </th>
                <th class="sticky test3">
                    Test3
                </th>
                <th>
                    Test4
                </th>
                <th>
                    Test5
                </th>
                <th>
                    Test6
                </th>
            </tr>
        </thead>
    </table>
</div>

Answer №2

To make this happen, simply incorporate a box-shadow into your sticky class like so:

.sticky {
    ....
    box-shadow: 2px 0px 0px white;
}

Answer №3

The problem is stemming from the use of border-collapse: collapse;
You will have better results by avoiding a table.
I've achieved the desired layout by utilizing div elements with display:inline-flex. Instead of specifying exact left values for each item, I wrapped the sticky elements in a single div.sticky.

.container {
  overflow: auto;
  width: 200px;
}

div.headers,
div.sticky {
  display: inline-flex;
}

div.headers>div,
div.sticky>div {
  background-color: red;
  border-right: 1px solid white;
  color: white;
  padding: 10px;
  z-index: 1;
}

div.headers>div.sticky {
  padding: 0;
  position: sticky;
  left: 0;
  border: none;
  z-index: 999;
}

div.sticky>div {
  background-color: blue;
}
<div class="container">
  <div class='headers'>
    <div class="sticky">
      <div>
        Test1
      </div>
      <div>
        Test2
      </div>
      <div>
        Test3
      </div>
    </div>
    <div>
      Test4
    </div>
    <div>
      Test5
    </div>
    <div>
      Test6
    </div>
  </div>
</div>

Answer №4

Make the sticky style more appealing with these changes:

  .sticky {
     background-color: navy;
     position: sticky;
     z-index: 2;
     border-spacing: 0 !important;
     outline: 1px dashed white;
     border: none;
  }

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

Having Trouble with Form Submission Button Across Different Web Browsers

Having some trouble with my form - all fields are properly closed with tags, but when I click the submit button, nothing happens. The page is loaded with code, so here's the link for you to check it out. Unfortunately, right-click is disabled, so ple ...

Are you looking for methods to alter the elements of a webpage prior to viewing it?

I have created a page with the intention of using it as a tool, but I am facing some challenges due to my limited experience in this field. As a newcomer, I am struggling to achieve certain goals: - My objective is to modify the values of an element on a p ...

Tips for personalizing the Material-UI sticky header to work with horizontal scrolling

Check out this example of a Material UI table with a sticky header on CodeSandox: https://codesandbox.io/s/yi98d?file=/demo.tsx I encountered an issue where the left side header cells slide behind each other when I added a stickyHeader prop to the Materia ...

Is there a way to confine a jquery script within the dimensions of the container div?

I have recently created a gallery with navigation buttons in jQuery. As a newcomer to jQuery, I wrote a small script that adjusts the margin of a div container by a fixed amount. The script is functioning properly, but it extends beyond the top and bottom ...

Does the onChange event fire when the value is modified by the parent element?

let [number, set_number] = useState({x: 1}); <ChildComponent number={number} onUpdate={onUpdateFunction} </ChildComponent> set_number({x: 2}) After running set_number({x: 2}), will this action prompt the execution of onUpdateFunction refere ...

Ways to eliminate the top space in the background image

After implementing a basic HTML code to add a background image to my webpage, I noticed there is still some empty space at the top of the page. I attempted to adjust the position of the background image using the following code, but it only lifted it upwar ...

Unable to add the div using a jQuery click event

Hey there, I'm looking to add a div dynamically when clicking on another div. Check out the code snippet below: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title></title> <script src= ...

Applying CSS dynamically to a mat-cell in Angular 6

In my material table, I need to apply specific CSS classes based on the content of the cell. These are the CSS classes that I am using .status-code{ flex: 0 0 10% !important; width: 10% !important; } .status-code-success{ flex: 0 0 10% !impo ...

When attempting to refresh, the global.Exp.Data.getLocalDataSet() function returns empty

In the first method, data is retrieved from the database and populated into the grid. The second method is used for viewing user details. When the user clicks on this method, it displays the user details. However, if the page is refreshed at that time, v ...

What is the best way to transfer variables to a different page through a pop-up window?

I'm working with a function that converts the Id of the clicked element into a variable, then opens a new window with a different page. How can I access or utilize this variable on the newly opened page? var idToWrite = []; $(function(){ $(".szl ...

Tips for animating the box-shadow effect using jQuery

Can anyone provide insight on how to animate the box-shadow of multiple elements? I have reviewed previous threads such as this one, but found outdated and non-working solutions. Additionally, I came across the jquery box-animation plugin, which is limit ...

Positioning the comments box on Facebook platform allows users to

Need assistance, I recently integrated the Facebook comments box into my Arabic website, but I am facing an issue where the position of the box keeps moving to the left. Here is an example of my website: Could someone please suggest a solution to fix the ...

Video background is malfunctioning on Firefox and Internet Explorer

Currently, I am facing some issues with the JQuery plugin 'videoBG'. The problem is that when I view the video offline, it works perfectly in all browsers. However, once I go online, the video stops working in FireFox and IE. It seems like the sc ...

Adding content to a text field and then moving to the next line

I am looking to add a string to a text area, followed by a new line. After conducting some research, here are the methods I have attempted so far but without success: function appendString(str){ document.getElementById('output').value += st ...

Error encountered during file upload - file field :input not found

I'm dealing with some HTML that looks like this: <div id="promocodesUpload"> <div class="qq-uploader"> <div class="qq-upload-drop-area" style="display: none;"> <span>Drop </span> </div> <a class=" ...

What's the reason behind the absence of applied bootstrap style in jsFiddle?

Link to the code: Click here I'm having trouble understanding why the default style of <ol> from Bootstrap is not being applied in this particular code. Any insights? Here's a snippet of the HTML code taken from the fiddle. <ul> ...

JavaScript: The functionality of calling functions through buttons ceases to function once the page is updated without reloading

I am trying to create a program that consists of one HTML page, where I can dynamically update and populate it with different elements using JavaScript. The main feature of the program is a button that remains constant in every version and displays a mod ...

How to align Font Awesome icons inside `<span>` vertically with each other

Trying to vertically align multiple font awesome icons: ############################# icon icon icon icon icon icon ############################# <div class="announcement-card-body modal-body card border-0"> <span class="info fa fa-info- ...

What is the best way to locate and extract the content of an HTML element using XPath in Selenium with VBA?

I am currently seeking a solution to extract the content of an element with the name "data-testid" from a website. This specific element appears approximately 35 times within various contexts in the HTML code. The particular element I am interested in look ...

Show the jQuery code block as HTML within a textarea

I'm in need of a way to display script reference code in a textarea for easy copying by users. <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"/> Even though I am using jQuery, all the solutions I ...