HTML table sticky header with borders
Krzysztof Żuraw

Krzysztof Żuraw @kzuraw

About: TypeScript & React + V60

Location:
Wrocław
Joined:
Oct 20, 2017

HTML table sticky header with borders

Publish Date: Jul 14 '22
8 1

I recently had to create an HTML table with a sticky header. It turns out you need this piece of CSS to make it work:

thead {
  position: sticky;
  top: 0;
  background: white;
  text-align: left;
}
Enter fullscreen mode Exit fullscreen mode

Which is fine and awesome - but what if your header needs to have a border at the bottom? Adding border-bottom property won't work if header is sticky. After a few trial and error hours I found out (with help of Mateusz) that you can add empty table row under your table head:

<tr>
  <th style="height:1px; padding:0px; color:black" colspan="6"></th>
</tr>
Enter fullscreen mode Exit fullscreen mode

See this in action in this Codepen:

Comments 1 total

  • Eric Esterling
    Eric EsterlingJul 1, 2025

    Might not be the best choice because it is a-semantic. But if you add "aria-hidden: true" to the tr, it would help.

    Changing table to "border-collapse: separate" helps in some circumstances.

    Others are using a box-shadow to emulate a border. It's weird, but seems to be effective.

Add comment