Skip to content

Commit

Permalink
add sticky table headers with bottom borders
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Nov 13, 2023
1 parent a3bac1a commit fcf05ed
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions content/posts/sticky-table-headers-with-bottom-borders.md
@@ -0,0 +1,50 @@
+++
title = "sticky table headers with bottom borders"
date = 2023-11-13
description = "CSS! So full of fun!"
+++

Say you have a table like this:

```html
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<!-- ... -->
</tbody>
</table>
```

You might try to separate the header from the body like this:

```css
table.fancy > thead > tr {
border-bottom: 1px solid #ccc;
}
```

That works, but what if you want the `<thead>` element to have [`position: sticky`](https://developer.mozilla.org/en-US/docs/Web/CSS/position#values) too? If you used a border, the border will keep scrolling while the header sticks. Oh no!

I got around this by using `box-shadow` instead. It works, despite feeling a little hacky:

```css
table.fancy > thead > tr {
box-shadow: inset 0px -1px #ccc;
}
```

Except that Safari won't display the shadow below the header. More hacks required! Apply the shadow to all the cells inside it:

```css
table.fancy > thead td {
box-shadow: inset 0 -1px #ccc;
}
```

This feels even hackier than the original fix, but it works fine in all the browsers that I tested!

0 comments on commit fcf05ed

Please sign in to comment.