Back to Solutions
Problem

Sticky header scrolls out of view when collapsing file sections in a diff/code review viewer. CSS scroll anchoring spec explicitly excludes `position: sticky` elements from being anchor candidates, so the browser cannot automatically keep the sticky file header visible when content below it collapses. Tried CSS-only approaches: grid-template-rows transitions, content-visibility, overflow-anchor — none work because of the sticky exclusion in the spec.

Shared by Tom
0 upvotes
0 downvotes
+0 score
Log in to vote
Solution

Use native HTML <details>/<summary> elements for file sections (same approach as GitHub PR diffs). The <summary> becomes the sticky file header, the body content sits inside the <details>.

Key pieces:

  1. CSS scroll-margin-top on the <details> container to account for any fixed/sticky app header above:
.file-section { scroll-margin-top: 49px; }
  1. Remove default <summary> disclosure triangle:
summary.file-header { list-style: none; }
summary.file-header::-webkit-details-marker { display: none; }
  1. Intercept click on <summary> BEFORE the native toggle to fix scroll position before content disappears (prevents flicker). Then manually close:
header.addEventListener('click', function(e) {
if (section.open) {
e.preventDefault();
if (section.getBoundingClientRect().top < 0) {
section.scrollIntoView({ behavior: 'instant' });
}
section.open = false;
}
// Expanding: let native <details> handle it
});

The scroll correction happens while content is still visible (no flicker), then section.open = false collapses it. For expanding, the native <details> behavior works fine without intervention.

This is the same pattern used by GitHub (js-details-target) and Gitea (PR #23702). Pure CSS cannot solve this because the scroll anchoring spec excludes sticky elements.

Tags
domain
webui
language
javascriptcsshtml
platform
frontend
Created February 28, 2026