7

Making Tables With Sticky Header and Footers Got a Bit Easier | CSS-Tricks

 2 years ago
source link: https://css-tricks.com/making-tables-with-sticky-header-and-footers-got-a-bit-easier/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Making Tables With Sticky Header and Footers Got a Bit Easier

Chris Coyier on

Jun 14, 2021 (Updated on Jun 15, 2021)

— Open, multi-cloud stack for modern apps

It wasn’t long ago when I looked at sticky headers and footers in HTML <table>s in the blog post A table with both a sticky header and a sticky first column. In it, I never used position: sticky on any <thead>, <tfoot>, or <tr> element, because even though Safari and Firefox could do that, Chrome could not. But it could do table cells like <th> and <td>, which was a decent-enough workaround.

Well that’s changed.

I heard through the Twitter that Chrome "rewrote tables" in v91.https://t.co/vTBplXWWtT

I saw it dropped, upgraded, and did a quick test. HEY LOOK STICKY TABLE HEADERS AND FOOTERS.@CodePen https://t.co/8eCP1GMer0

(Works in Safari and Firefox too) pic.twitter.com/rBN3bXm5iD

— Chris Coyier (@chriscoyier) June 4, 2021

Sounds like a big effort went into totally revamping tables in the rendering engine in Chromium, bringing tables up to speed. It’s not just the stickiness that was fixed, but all sorts of things. I’ll just focus on the sticky thing since that’s what I looked at.

The headline to me is that <thead> and <tfoot> are sticky-able. That seems like it will be the most common use case here.

table thead,
table tfoot {
  position: sticky;
}
table thead {
  inset-block-start: 0; /* "top" */
}
table tfoot {
  inset-block-end: 0; /* "bottom" */
}

That works in all three major browsers. You might want to get clever and only sticky them at certain minimum viewport heights or something, but the point is it works.

I heard several questions about table columns as well. My original article had a sticky first column (that was kind of the point). While there is a table <col> tag, it’s… weird. It doesn’t actually wrap columns, it’s more like a pointer thing to be able to style down the column if you need to. I hardly ever see it used, but it’s there. Anyway, you totally can’t position: sticky; a <col>, but you can make sticky columns. You need to select all the cells in that column and stick them to the left or right. Here’s that using logical properties…

table tr th:first-child {
  position: sticky;
  inset-inline-start: 0; /* "left" */
}

Here’s a sorta obnoxious table where the <thead>, <tfoot>, and the first and last columns are all sticky.

I’m sure you could do something tasteful with this. Like maybe:

A `sticky` table row/column can be such a nice touch. Here's an example from @destroytoday https://t.co/uN3nIisHxF pic.twitter.com/KttE5puBqZ

— Chris Coyier (@chriscoyier) June 2, 2021


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK