The sticky positioning does not function properly when used with the display property set to table-row-group in

I have a table with data that I want to keep the header sticky.

The structure of my table is as follows:

HTML

<div class='table' id='table'>
      <div class='header row'> <!-- removing row class, although functional, makes it look unattractive -->
        <div class='cell'>Col A</div><div class='cell'>Col B</div>
      </div>
      <div class='row'>
        <div class='cell'>x</div><div class='cell'>x</div>
      </div>
    </div>
    

Minimal CSS

.table {
      display: table;
    }
    .row {
      display: table-row-group; /* removing this, despite functionality, affects aesthetics */
    }
    .header {
      position: -webkit-sticky;
      position: sticky;
      top: 0;
    }
    .cell {
      display: table-cell;
    }
    

The table appears as desired, but encountering some issues when trying to implement the sticky header feature.

If I remove display: table-row-group; from the row class, the sticky header works, but the table looks unpleasing.

Similarly, eliminating the row class from the header makes the sticky feature operational, yet impacts the visual appeal of the table.

Link to live example: https://jsfiddle.net/mexw58yp/4/

Inquiry

Is there a way to retain the current div-based table structure and still achieve the sticky header effect without using JavaScript, solely via CSS?

Additionally, is there a method to avoid incorporating traditional table elements like table, tr, td, etc.?

Clarification

I am running Chrome version 72. Testing on Firefox seemed fine. Uncertain about the implications of this discrepancy.

Answer №1

You can also utilize the child selector for sticky elements using .header:nth-child(1) .cell

var table = document.getElementById('table');

for (var j = 0; j < 20; j++) {
var row = document.createElement('div');
row.classList.add('row');

for (var i = 0; i < 2; i++) {
var cell = document.createElement('div');
cell.classList.add('cell');
cell.textContent = 'abcde';
row.appendChild(cell);
}

table.appendChild(row);
}
.table {
display: table;
font-size: 16px;
}
.row {
display: table-row-group; /* removing this, it works but table off kilter */
}
.header:nth-child(1) .cell {
position: -webkit-sticky;
position: sticky;
top: 0;
background-color: #fc0;
}
.cell {
display: table-cell;
border-bottom: 1px solid black;
border-right: 1px solid black;
padding: 5px;
}
.cell:nth-child(1) {
border-left: 1px solid black;
}
.row:nth-child(1) .cell {
border-top: 1px solid black;
}
<div class='table' id='table'>
<div class='header row'>
<div class='cell'>Col A</div><div class='cell'>Col B</div>
</div>
</div>

Answer №2

To make the header table-cell div sticky, simply add the 'sticky' property in the CSS code. For a straightforward demonstration, you can view this JSFiddle example.

.td.header {
  position: sticky;
  top:0px;
}

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

Tips for showcasing API responses in HTML

Although I have successfully retrieved the data, I am facing issues with efficiently displaying it on an HTML page. Currently, my solution involves using two endpoints - one to fetch API data and another to load the HTML with a script. However, this appro ...

Tips for customizing the color scheme of background, rows, row selection, and headers in Angular 4 using Bootstrap 4 data tables

I've integrated Bootstrap 4 data table in my Angular 4 project, but I'm struggling to customize row colors, row selection colors, and header colors. You can check out the example of the data table I'm using at this link: https://github.com/ ...

Ensure that the div spans the entire width of the page, prioritize maintaining the aspect ratio, but allow for variable

I have a dilemma with a div containing multiple elements. My goal is to make the div 100% in width, while also attempting to preserve the aspect ratio if feasible. Should the enclosed elements exceed the div's boundaries, then the height will adjust a ...

Customized placement of form fields on an HTML grid determined by the user

My goal is to organize input elements on a grid based on user preferences. After researching, I stumbled upon CSS grids, which seem promising. I am considering creating a CSS grid with r rows and c columns, then using JavaScript to assign input elements t ...

Removing the gridlines in a Linechart using Apexcharts

I am experiencing issues with the grid and Nodata options on my Apexchart line chart. noData: { text: none , align: 'center', verticalAlign: 'middle', offsetX: 0, offsetY: 0, style: { color: undefined, fontSize: &apo ...

Issue with Three.js GLTF loader peculiar behavior while attempting to incorporate three-dimensional text

I had an idea to import a prebuilt gltf scene created in Blender, then add some 3D text using the ttf loader and manipulate it. Each loader worked perfectly when used separately, but strange things started happening when I combined them into a single scrip ...

Designing Your WordPress Website's Header Structure

When working on my non-WP sites, I usually handle columns using Bootstrap. However, this is my first time delving into CSS positioning without it. I am trying to align my site header elements similar to the image shown, where the text elements are flush ri ...

Failed to load CSS file in nodeJS application

I followed a tutorial to create a backend app using nodeJS and Express. My MongoDB connection with Mongoose is working fine. However, I am facing issues when trying to add a simple front-end using html/ejs/css. The endpoints are loading on localhost but on ...

What can be done to prevent parallel images from getting displaced while using CSS3 transformation effects?

Currently facing an issue where three separate images are being transformed using CSS3 upon hover. Various attempts have been made, including placing them in divs, a table, or just plain images. Trying to float them all in one direction, with #1 left, #3 ...

Recursively update the image of the table header

I have been using the jquery.sortElement.js plugin to sort a table. The sorting functionality based on the header is working correctly. However, I now want to enhance it by adding an image that changes based on whether the order is ascending or descending. ...

Disabling dynamically generated buttons in ASP.NET using jQuery

I am facing an issue with creating a dynamic button in my view based on a state. Index.cshtml: <html> <head> <script src="~/scripts/jquery-3.3.1.js"></script> <script src="~/scripts/jquery-3.3.1.min.js"></script&g ...

The range input in HTML does not respond when attempting to adjust it

I am having an issue where the thumb on the slider does not move when I try to. This problem occurred after I inserted an img element inside the slider-cntnr div. View the code at http://codepen.io/danielyaa5/pen/xVEXvB HTML <div id="slider-cntnr"&g ...

Utilize the Sed command to manipulate the input stream by substituting all occurrences of HTML <i> tags with <em> tags

I am attempting to create a regex with the sed command in order to process the input stream and replace all HTML tags with em tags. For example: This is <i id="x">emphasized text</i> and <i>so is this</i>. should be replaced by Thi ...

Troubleshooting: AngularJS routing issue

I'm facing an issue with my AngularJS routing code. Here is the code snippet: /// <reference path="C:\Users\elwany\documents\visual studio 2015\Projects\spaapplication\spaapplication\scripts/angular.js" /& ...

Issue with spacing when assigning a JavaScript array variable to a value within input tags during a loop

Having some trouble with JavaScript code where the value is not passing in its entirety if there are spaces between the words. How can I make sure the full string or array object gets passed into the input tag's value? This is how I am trying to assi ...

Tips on creating a transition in React to showcase fresh HTML content depending on the recent state changes

I am completely new to React and recently completed a project called Random Quote Machine. The main objective was to have a method triggered inside React when the user clicks a button, changing the state value and re-rendering to display a new quote on the ...

The visual effects of CSS 3D transformations vary between PCs and mobile devices, creating a distinct contrast

I'm experiencing a rendering issue with a 3D transform. On Chrome (PC/Mac), it appears as shown below: https://i.sstatic.net/pDbwN.png However, on mobile devices using Safari or Chrome, the display is different: https://i.sstatic.net/1mQHl.jpg When ...

Inject HTML entities, escaped for CSS, dynamically using JavaScript

My goal is to dynamically generate a list of HTMLElements with unique data-* attributes that correspond to various HTML Entities. These attributes will then be utilized by CSS to display content in pseudo elements like this: li:after { content: attr(dat ...

Displaying Table Headers on Page Breaks in Laravel PDF with Webkit Technology

Let me provide some context: I am utilizing Laravel to generate a view that is then converted to a PDF using barryvdh/laravel-snappy with the help of wkhtmltopdf, and everything is functioning as expected. However, I am encountering difficulties in stylin ...

Erase periods from the text box

Is there a way to remove the dots in the right bottom corner of a textarea without disabling resizing? https://i.sstatic.net/N2zfh.jpg I have seen a solution using the css property resize:none, but I would like to keep the resizing functionality... Are ...