How can I use CSS to make the row and column headers of a table freeze while scrolling within an overflow

I am currently working on implementing frozen row and column headers in a table within a content wrapper, similar to what you see in Excel. However, I have been facing challenges with using CSS tricks that involve the position:absolute property, especially when there are other shifting elements on the screen.

My goal is to have the row and column headers (represented by the red, green, and purple boxes) stay fixed in place while the user scrolls or pans through the white region of the table. The gray area surrounding the content cannot be removed.

It is likely that the content will stretch vertically beyond the initial view, so ideally, I would like the column headers to stick to the top as the user scrolls down. However, it is not a mandatory requirement for me. Even if the headers just remain in their original position when the page is loaded, I would appreciate it.

In addition, I want the row headers to remain static while scrolling horizontally causes the column headers to move along with the content.

If there is a reliable way to achieve this functionality, I would greatly appreciate any guidance. I have created a JSFiddle to demonstrate the scenario within the content wrapper: http://jsfiddle.net/Jr5Zt/3/

<table id="bodyTable">
    <tbody><tr><td id="bodyCell">
        <!-- my custom content -->
    </td></tr></tbody>
</table>

Answer №1

I have yet to discover a pure HTML/CSS solution that accomplishes the following:

  • utilizes tables semantically
  • addresses both header and column alignment
  • works with variable width columns
  • functions with vertical and horizontal scrolling

Here is a method I devised that does work:

http://codepen.io/saiidi/pen/mePdqo

The issue I find with it is the duplication of content in the header th elements. With some additional tweaking, this could potentially be resolved.

In the interest of including code in the response, here is the code:

HTML:

<div class="scrolly">
  <div class="scrollx">
    <table>
      <thead>
        <tr>
          <th class="sticky"><div>1</div></th>
          <th class="sticky"><div>2</div></th>
          <th>header 1<div>header 1</div></th>
          <th>header 2<div>header 2</div></th>
          ...
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="sticky">hello 1...</td>
          ...
        </tr>
        ...
    </table>
  </div>
</div>

CSS:

td, th {
  padding: 5px;
  white-space: nowrap;
}
...

JavaScript:

$(function() {
  $("#status").text('loaded');
  ...
});

Essentially, this implementation uses absolute positioning on fixed columns to keep them anchored to the left side. To accommodate varying widths, it calculates the width before setting the position to absolute. A margin is then applied to the left of the table container to adjust for the absolutely positioned columns.

For fixing the header, it applies absolute positioning to the nested divs within the th elements and updates their "top" property as the table is scrolled. The duplicated content accounts for accurate column width calculation based on the header content.

This approach is somewhat rudimentary - the jQuery code provided serves as a proof of concept.

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

Is there a way to make a glossy `selectInput()` extend beyond a `bslib::navset_card_pill()`?

The dilemma: My issue is that the selectInput() in my code gets cut off by the lower border of the card: library(shiny) # Version 1.7.4 library(bslib) # Version 0.5.0 ui <- page_fluid( navset_card_pill(nav_panel( title = "No overflow :(&quo ...

The ability to use the backspace and Ctrl + c (Copy) functions is restricted in input fields on FireFox

I have a form with input fields designed using semantic UI. I need the input field to only accept numbers, remove spaces, and allow copying from it using ctrl +c. After some investigation, I found this jQuery code that seems to satisfy my requirements per ...

Establishing a secondary setTimeout function does not trigger the execution of JQUERY and AJAX

// Custom Cart Code for Database Quantity Update $('.input-text').on('keydown ' , function(){ var tr_parent = $(this).closest("tr"); setTimeout(function () { $(tr_parent).css('opacity', '0.3'); }, 4000); var i ...

Finding your current location and address using HTML5 geolocation

My web application utilizes HTML5 geolocation to obtain latitude and longitude coordinates. I am looking for a way to convert these coordinates into actual addresses whenever possible. Once that is done, I need to save the address information into a MySQ ...

Utilizing React Router with HTML5 pushstate in conjunction with Nginx servers

I am working on a React app with react-router and HTML5 pushstate routes. I want to use http://example.com/v2 as the base route for serving my new website. Below are the nginx configurations that I am using: This configuration is designed to catch reques ...

Increase the spacing between rows and columns in Bootstrap

Is there a way to create gaps between rows and columns in bootstrap without disrupting the layout? I am trying to have three columns on each row by using col-4, but when I try to add margin or padding, it messes up the design. My goal is to achieve a layo ...

Issue encountered while transferring files between a web platform and an API

Currently, I am working on two separate projects. The first project involves a website where users can upload files, and the second project is an API designed for file uploads due to the limitations of the old website. I am utilizing Laravel (6) to develo ...

When attempting to retrieve the value of my select element, I encounter difficulty due to a hidden field that only becomes visible when a certain option is selected

I have a dropdown menu with different options, including one labeled "other". When the user selects "other", a hidden text field appears for them to enter a custom value. This functionality works correctly. However, if the user selects any other option and ...

Spacing between columns in Bootstrap5 for responsive design

In my Django app, I am utilizing Bootstrap for styling columns. I have successfully created a responsive layout that stacks vertically after a specific breakpoint using classes. Now, my goal is to add some space or gutter between the columns while maintai ...

What is the best way to begin the main page content below the stationary header in order to prevent any overlap with the hyperlink section?

I have created this HTML page, but I am facing an issue where the hyperlinked sections overlap with the static header when clicked on. I want all linked sections to appear below the header and each section to have different dimensions within the iframe. I ...

Ways to adjust dimensions depending on zoom level or screen size

Here is the HTML code snippet: <div id="divMainFirst"> <div id="divInnerFirst"> <div id="divTitleHeaderUC"> <span id="spanTitleHeaderUC">Urgent Care Wait Time</span> </d ...

Issues with the CSS in our unique eBay template are creating some challenges

Our team is currently in the process of creating a customized template for our eBay listings. eBay provides users with the option to upload a description as HTML code and allows the code to link external CSS files. When uploading HTML code on eBay, it ap ...

Center text within a span element

I'm struggling to set up arrow navigation with both next and previous buttons that need to function in Internet Explorer 7 and newer versions. For the next button, I want the background image to appear to the right of the text. As for the previous bu ...

Tips for converting plain text output into HTML tags

I recently set up a contact form 7 on my website, and I'm trying to customize the message that appears after submission. However, I'm running into an issue when attempting to split the message into two different colors. I created a span class to ...

Adjust the autofocus to activate once the select option has been chosen

Is there a way to automatically move the cursor after selecting an option from a form select? <select name="id" class="form-control"> <option>1</option> <option>2</option> <option>3</option&g ...

The design I created includes a horizontal scroll feature and certain elements are not properly aligned in the

Oh man, I'm really frustrated right now. I want all my elements to be centered horizontally on smaller devices, but some of them are slightly off to the side. And to top it off, there's a horizontal scroll appearing - it's like something is ...

Eliminate HTML nodes that are not currently visible

I am currently developing a Vue/Vuetify application that showcases a large number of images (10-20k) in a grid view along with some additional information. These images are grouped into different sections, and clicking on an image opens an overlay displayi ...

Is it possible to use the same identifier for both the name and id attributes in HTML?

In the world of coding, the "name" attribute is often used in server-side programming to send name/value pairs in requests. On the other hand, the "id" attribute is commonly utilized in client-side programming such as Javascript and CSS. However, both att ...

AngularJS fails to prioritize the following element

Here is my HTML code: <div ng-app="app"> <form> <div class="form-group"> <label >Username</label> <input focus type="text" class="form-control" id="loginUserName" ng-model="userForm.crn" required/> ...

What is the best way to remove excess content that falls outside the viewBox?

Is there a function or method to trim a path that extends beyond the viewbox rather than simply concealing it? I am currently using svg-edit, which has a specific viewbox or canvas area. Any elements drawn outside of this canvas remain hidden. However, wh ...