{"id":303595,"date":"2020-02-21T14:48:49","date_gmt":"2020-02-21T21:48:49","guid":{"rendered":"https:\/\/css-tricks.com\/?p=303595"},"modified":"2020-02-21T16:49:30","modified_gmt":"2020-02-21T23:49:30","slug":"fixed-headers-and-jump-links-the-solution-is-scroll-margin-top","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/fixed-headers-and-jump-links-the-solution-is-scroll-margin-top\/","title":{"rendered":"Fixed Headers and Jump Links? The Solution is scroll-margin-top"},"content":{"rendered":"\n

The problem: you click a jump link like <a href=\"#header-3\">Jump<\/a><\/code> which links to something like <h3 id=\"header-3\">Header<\/h3><\/code>. That’s totally fine, until you have a position: fixed;<\/code> header at the top of the page obscuring the header you’re trying to link to!<\/p>\n\n\n\n

Fixed headers have a nasty habit of hiding the element you’re trying to link to. <\/p>\n\n\n\n\n\n\n\n

\"\"<\/figure>\n\n\n\n

There used to be all kinds of wild hacks<\/a> to get around this problem. In fact, in the design of CSS-Tricks as I write, I was like, “Screw it, I’ll just have a big generous padding-top<\/code> on my in-article headers because I don’t mind that look anyway.” <\/em><\/p>\n\n\n\n

But there is actually a really straightforward way of handling this in CSS now. <\/p>\n\n\n\n

h3 {\n  scroll-margin-top: 5rem; \/* whatever is a nice number that gets you past the header *\/\n}<\/code><\/pre>\n\n\n\n

We have an Almanac article on it<\/a>, which includes browser support, which is essentially everywhere. It’s often talked about in conjunction with scroll snapping<\/a>, but I find this use case even more practical. <\/p>\n\n\n\n

Here’s a simple demo:<\/p>\n\n\n\n