brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: scroll-margin-top keeps anchor targets from hiding under a sticky css header

Css
~/tips/tip-css-scroll-margin-top-anchors

h2[id], h3[id] { scroll-margin-top: 5rem; }

Leaves space above a link target so a fixed header does not cover it after scrolling.

Clicking a table-of-contents link and finding the heading behind the header is annoying.

h2[id], h3[id] { scroll-margin-top: 5rem; }
/* or once for the whole page */
html { scroll-padding-top: 5rem; }

When the browser scrolls to a fragment such as #install, scroll-margin-top on the target leaves that much space above it. Set it to a bit more than the height of your sticky header. The alternative is scroll-padding-top on the scrolling element (usually html), which applies to all anchors at once. Both also affect scroll snapping, so keep that in mind if you combine them with scroll-snap-type. Reading the value from a CSS variable keeps the header height in one place.

Further reading: MDN: scroll-margin-top

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link