0

I have a floating HTML table header, which remains at the top when the table is being scrolled through. I have implemented in by cloning the header of the original table and then hiding / showing it based on the scroll position.

When my table had a few columns, the "floating-header" had the same column width as the columns in the original table, due to this code:

$headerClone.children().outerWidth(function(i,val){
    return $headerGrandChildren.eq(i).outerWidth(true);
});

However, now that the table has more columns and goes over the page right margin, the "floating-header" width is being adjusted and squeezed, disregarding the set width=x style element and the table-layout: fixed; CSS, see screenshot:

enter image description here

I have read that "The browser will then set column widths based on the width of cells in the first row of the table" in here.

Question: how to best set the fixed size of the "floating-header"? Should I create an invisible row that would accompany the "fixed-header" so it would not resize as soon as my table is wider than the page?

Milad Rashidi
  • 1,296
  • 4
  • 22
  • 40
Mindaugas Bernatavičius
  • 3,757
  • 4
  • 31
  • 58
  • It would be a heck of a lot easier just to use `position: fixed` to ensure your header stays at the top of the page. Also, if you insist on using tables, I'd also recommend setting a `max-width` of `100%` to prevent it from needing a horizontal scroll. – Obsidian Age Dec 12 '17 at 19:57
  • You can use this plugin or get the idea from it: https://github.com/jmosbech/StickyTableHeaders – phobia82 Dec 12 '17 at 20:07
  • Can you show simplified html part of your table? – Milad Rashidi Dec 12 '17 at 20:20
  • @ObsidianAge - when I add the `possition: fixed` to the `` the header does not look like part of the table anymore, the widths of each column is not the same as for the table itself. – Mindaugas Bernatavičius Dec 12 '17 at 20:49
  • Woah, you shouldn't be using a `` at *all* for a "header", and you certainly shouldn't be adding the `position` property to the `` element. It would go on the `
    ` used to *contain* the `
    `. I'd recommend avoiding table-based layouts entirely if at all possible, as they're not built for responsiveness.
    – Obsidian Age Dec 12 '17 at 20:55