Fixed Header Table

Whenever I attempt to set the table header at a position other than 0, the body ends up scrolling above the header. Below is the code snippet showcasing the issue:

.test {
  padding-top: 50px;
}

th {
  background-color: orange;
  position: sticky;
  top: 50px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="test">
  <table class="table">
    <thead>
      <tr>
        <th>column1</th>
        <th>column2</th>
        <th>column3</th>
        <th>column4</th>
      </tr>
    </thead>
    <tbody>
     // rest of the HTML table rows...
    </tbody>
  </table>
</div>

Trying out another approach, I added a TH element with a white background color before the column names. Here's the current code snippet I'm using:

.test {
  padding-top: 0px;
}

th {
  background-color: orange;
  position: sticky;
  top: 50px;
}

.spacer {
  background-color: white;
  position: sticky;
  top: 0px;
  height: 50px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="test">
  <table class="table">
    <thead>
      <tr>
        <th colspan="4" class="spacer"></th>
      </tr>
      <tr>
        <th>column1</th>
        <th>column2</th>
        <th>column3</th>
        <th>column4</th>
      </tr>
    </thead>
    <tbody>
     // rest of the HTML table rows...
    </tbody>
  </table>
</div>

I'm still facing issues and wondering if there's a better solution. Moving the body didn't yield the desired result.

Answer №1

If you want to add space without using padding, consider utilizing a pseudo element instead. This will not only create the desired space but also make it sticky:

.test {
  position:relative;
}
.test::before {
 content:"";
 display:block;
 background:#fff;
 position:sticky;
 z-index:3;
 top:0;
 height: 50px;
}

th {
  background-color: orange;
  position: sticky;
  top: 50px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="test">
  <table class="table">
    <thead>
      <tr>
        <th>column1</th>
        <th>column2</th>
        <th>column3</th>
        <th>column4</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
      </tr>
      <tr>
        <td>2</td>
        <td>2</td>
        <td>2</td>
        <td>2</td>
      </tr>
      <tr>
        <td>3</td>
        <td>3</td>
        <td>3</td>
        <td>3</td>
      </tr>
      ... (continued)
    </tbody>
  </table>
</div>

Answer №2

I have made a modification to the table class by adding position relative and fixing it

.test {
  padding-top: 0px;
}

th {
  background-color: orange;
  position: sticky;
  top: 0;
}

.spacer {
  background-color: white;
  position: sticky;
  top: 0px;
  height: 50px;
}
.table{
  position: relative;

}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="test">
  <table class="table">
    <thead>
      <tr>
        <th colspan="4" class="spacer"></th>
      </tr>
      <tr>
        <th>column1</th>
        <th>column2</th>
        <th>column3</th>
        <th>column4</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
      </tr>
     <!-- More rows -->
    </tbody>
  </table>
</div>

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

Combining v-on:click and v-link in Vue.js

I'm currently working on a Vue.js application and I am in the process of creating a login system that involves multiple components. Within my App.vue component, which serves as the main component with the navigation bar, there is a button that looks ...

Trigger JavaScript keypress event on keypress

In my code, I have a function called addMoney() that is triggered by pressing Enter in an input field: $('body').on('keypress', 'input[type=text]', function(e){ if(e.keyCode==13){ var val = $('input[type=te ...

Injecting sinon js into an application within an iFrame: a step-by-step guide

I am looking to implement ajax requests testing in my application. The application is running within an iframe, and my goal is to have the iframe's wrapper page test the application using sinon (which will send the response). I attempted to include ...

Unequal height among each div's elements

I'm struggling with creating a footer divided into 3 blocks, but they do not have equal heights, causing the border-right line to appear uneven. Here is a screenshot of the issue: Not equal height of elements Any suggestions on how to fix this? Is m ...

Storing a token in NodeJS using JavaScript

We currently have a mobile single-page application built using HTML/CSS/NodeJS. The functionality of this app requires numerous API calls, all of which require a bearer token for authorization purposes. This bearer token is simply a string value that we ge ...

Tips for eliminating objects with a sessionID:null value from the nested array within an array of objects using JavaScript

[ { "_id": "5ecda7f5310bee6f4b845add", "firstname": "john", "lastname": "smith", "sessions": [ { "_ ...

Merging multiple observables with RxJs forkJoin

UPDATE : I'm currently facing a challenging issue that I can't seem to resolve. Within my code, there is a list of objects where I need to execute 3 requests sequentially for each object, but these requests can run in parallel for different obje ...

Challenges with looping in Jquery animations

I've been working on an animation function that I want to run in a loop. So far, I've managed to get it to work for one iteration, but I'm struggling to figure out how to repeat the loop a specific number of times. Below is the code for my a ...

Tests are not visible to jasmine-node

Currently, I am utilizing jasmine-node and running it with the following command: node.exe path/to/jasmine_node --verbose path/to/my_file.js Despite successfully invoking Jasmine-node and receiving an error for incorrect paths, it appears that no tests a ...

No default export function available on this page

Recently delving into Nextjs, I'm currently in the process of setting up a MongoDB connection using middleware configuration. Let me showcase my code: import type { NextApiRequest, NextApiResponse } from 'next' import { connectMongoDB } fro ...

Loading a chunked polyfill file in webpack only when needed - a step-by-step guide

In order to prevent unnecessary loading of polyfills, it is recommended to add some logic in the <head> section (https://webpack.js.org/guides/shimming/) <script> var modernBrowser = ( 'fetch' in window && ...

Creating a Dropdown Selection Menu Dynamically with jQuery and SQL Query结果

I have a website that provides information from a database based on user-set parameters. Currently, the SQL Statement is loaded from a file to populate a dropdown menu with options. The HTML code used for this process is displayed below: while($rec = $con ...

Small jumps occur when implementing the scrollTop jquery animation

I've encountered an issue with the scrollTop jquery animation that I can't seem to resolve. It seems to micro-jump right before the animation, especially when there is heavier content. I'm puzzled as to why this is happening... Here's ...

Ionic ion-view missing title issue

I'm having trouble getting the Ionic title to display on my page: http://codepen.io/hawkphil/pen/oXqgrZ?editors=101 While my code isn't an exact match with the Ionic example, I don't want to complicate things by adding multiple layers of st ...

history.push() function is ineffective within a JavaScript file that does not contain a class

I've been delving into React and encountering an issue with the history.push("/dashboard") method, it's not functioning as expected. import axios from "axios"; import { GET_ERRORS, GET_PROJECT, GET_PROJECTS } from "./types"; export const createP ...

Replacing strings with special characters appears to be malfunctioning

After spending countless hours searching through Stack Overflow and other resources, I am still unable to understand what is happening here. Any assistance would be greatly appreciated! I am trying to convert document.write('</div>'); to - ...

Creating a color gradient for hyperlinked text when hovered over: a tutorial

How can I achieve this effect using CSS only? I want the link color to change from #00C to #000 when hovered over, with a gradient transition taking 1 second. ...

What is the best way to automatically redirect to a different page once a video has finished playing after 10 seconds?

I'm working on creating an HTML page that will automatically redirect after 10 seconds once a video has finished playing. The issue I'm facing is that the page is currently redirecting as soon as it loads, rather than waiting for the video to fin ...

What steps should be taken to enable the "You and moderator can reply" feature in a bot when sending proactive messages to channels?

A project I am currently working on involves creating a bot that sends proactive messages to channels. The client has requested that I include options like No reply or You and moderator can reply with the messages posted by the bot proactively. Here is wh ...

elements that do not reside within a div with a float style

I am struggling with a basic CSS layout issue. I have a div container with content that I am trying to divide into two sections using the float property. Despite applying the float to the elements within the div, they do not stay inside as expected. https ...