← All Exercises
Sticky Header Without Hiding Anchor Targets
intermediatecss
Background
position sticky alone overlays anchor targets; scroll-padding and scroll-margin move the scroll destination out from under fixed interface.
Problem
Make a 4rem tall header stick to the top of the viewport while in-page anchor links still land with the target heading fully visible below the header.
Input
A page with a sticky header and three linked section headings.
Output
CSS for sticky positioning and scroll offsets.
Sample tests
Sample 1
Input: User scrolls down the page
Expected: header stays at top 0 with position:sticky and a z-index above article content
Sample 2
Input: User activates an in-page link
Expected: html scroll-padding-top 5rem keeps the heading below the 4rem header
Your solution
Learning objective
Combine sticky positioning with scroll-padding to keep anchor targets visible below pinned UI.