A sticky heading with a top inset
position: sticky;
top: 0;Sticky needs a non-auto inset. This heading uses top: 0 and does not place overflow on a wrapping ancestor.
- No overflow other than the initial visible is set on .page, so the intended scrollport is the viewport. This was not scrolled in a browser here.
<div class="page">
<h2 class="stuck">Weekly tasks</h2>
<p>The list continues below the heading.</p>
<p>More content so the page can scroll in a real viewport.</p>
</div>
<style>
.page { overflow: visible; }
.stuck { position: sticky; top: 0; background: #F3EFE4; }
</style>What this markup means
.stuck is sticky with top: 0, so the stick threshold is the top of the scrollport. .page leaves overflow at visible, which avoids introducing a nearer scroll container. If .page used overflow: hidden or auto, the heading would be constrained to that box instead.
Related: Box model