{"id":925,"date":"2020-02-25T18:40:20","date_gmt":"2020-02-25T23:40:20","guid":{"rendered":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/?p=925"},"modified":"2020-09-22T16:49:20","modified_gmt":"2020-09-22T20:49:20","slug":"scroll-margin-top","status":"publish","type":"post","link":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2020\/scroll-margin-top\/","title":{"rendered":"scroll-margin-top"},"content":{"rendered":"\n<p><strong>What:<\/strong> CSS-Tricks just posted about this last Friday and since I could have used this that day (!) but didn&#8217;t find it until now, I&#8217;m adding a note here for myself:<\/p>\n\n\n\n<p>You have a page with a table of contents (with links), or you have a one page website with in-page navigation <em>and<\/em> you have a fixed header &#8212; when you click a link the content scrolls up too far and is hidden by the fixed header. This is not a good user experience, and you can improve it with <code>scroll-margin-top<\/code> in Firefox and Chrome-based browsers&#8230;<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>How:<\/strong> Use <code>scroll-margin-top<\/code> on the item that is being covered by the fixed header, like so:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The CSS<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">header { position: fixed; top: 0; width: 100%; height: 6em; }\nh3 { scroll-margin-top: 6em; }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The HTML<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;header>My fab website&lt;\/header>\n&lt;main>\n  &lt;nav class=\"table-of-contents\">\n    &lt;ul>\n      &lt;li>&lt;a href=\"#topic-1\">Topic 1&lt;\/a>&lt;\/li>\n      &lt;li>&lt;a href=\"#topic-2\">Topic 2&lt;\/a>&lt;\/li>\n      &lt;li>&lt;a href=\"#topic-3\">Topic 3&lt;\/a>&lt;\/li>\n    &lt;\/ul>\n  &lt;\/nav>\n  &lt;section>\n    &lt;h3 id=\"topic-1\">Topic 1&lt;\/h3>\n    &lt;p>A whole bunch of text.&lt;\/p>\n  &lt;\/section>\n  &lt;section>\n    &lt;h3 id=\"topic-2\">Topic 2&lt;\/h3>\n    &lt;p>A whole bunch of text.&lt;\/p>\n  &lt;\/section>\n  &lt;section>\n    &lt;h3 id=\"topic-3\">Topic 3&lt;\/h3>\n    &lt;p>A whole bunch of text.&lt;\/p>\n  &lt;\/section>\n&lt;\/main><\/code><\/pre>\n\n\n\n<p><strong>Where:<\/strong> as said, Chris Coyier wrote about it @ <a rel=\"noreferrer noopener\" aria-label=\"CSS-Tricks (opens in a new tab)\" href=\"https:\/\/css-tricks.com\/fixed-headers-and-jump-links-the-solution-is-scroll-margin-top\/\" target=\"_blank\">CSS-Tricks<\/a><\/p>\n\n\n\n<p><strong>Thoughts:<\/strong> <a rel=\"noreferrer noopener\" aria-label=\"Support is very good (opens in a new tab)\" href=\"https:\/\/caniuse.com\/#search=scroll-margin-top\" target=\"_blank\">Support is not very good<\/a>: IE, Safari, iOS and old Edge all have no support.<\/p>\n\n\n\n<p>On Friday I decided, since it was a rush job and I didn&#8217;t have time for complex hacks or prolonged research, that in this case the table of contents was more important than the fixed header and I nixed the fixed header. Since cross-browser support is not very good, I still think that is the best solution in this case, but I might add some scroll-margin-top anyway, just to make it look a bit nicer, when people scroll&#8211;not quite as cramped.<\/p>\n\n\n\n<div class=\"_3bJ2H CHExY\">Photo credit: Photo of dusk at the Hudson River, NY NY, looking towards New Jersey. By <a rel=\"noreferrer noopener\" aria-label=\"me (opens in a new tab)\" href=\"https:\/\/www.flickr.com\/photos\/blind007\/\" target=\"_blank\">me<\/a>.<\/div>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-central-palette-4-background-color has-central-palette-4-color is-style-dots\"\/>\n\n\n\n<p><strong>EDITS:<\/strong> As a lovely person pointed out, I actually wrote <code>margin-scroll-top<\/code> in one instance instead of <code>scroll-margin-top<\/code>.<\/p>\n\n\n\n<p>The same lovely person also asked if I knew how to make this work in iOS and &#8230; that stopped me because I had in my mind that browser support was good. I checked this post and that was in fact what it said &#8220;Support is very good&#8221; even with a link to Can I Use which, when I looked again, yes, the Safari and iOS boxes where vaguely green but with those little numbers in them implying caveats&#8230; and the caveats mean, in this case, that it doesn&#8217;t work in Safari or iOS.<\/p>\n\n\n\n<p>You would have to use <code>scroll-snap-margin-top<\/code> instead of <code>scroll-margin-top<\/code> and this is because Safari has implemented it as part of the <em>CSS scroll snap module<\/em>, which means it is only to be used with scroll snap containers. Since that&#8217;s not what we&#8217;re doing here, it won&#8217;t work in Safari or iOS, I am sad to say.<\/p>\n\n\n\n<p>The simplest solution with good cross-browser support is still this one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">h3 {\n  padding-top: 6em;\n  margin-top: -6em;\n}<\/code><\/pre>\n\n\n\n<p>Source: <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/58824570\/css-scroll-padding-top-not-working-in-safari#answer-58890319\" target=\"_blank\">stackoverflow<\/a><\/p>\n\n\n\n<p>I am very sorry for my earlier sloppiness and I updated this post to make it clear right away. <\/p>\n\n\n\n<p>Also: thank you very much, lovely person who let me know! I&#8217;m sorry I don&#8217;t have better news, but I appreciate you emailing me about this. <\/p>\n\n\n\n<p>In general, comments and emails to let me know things like this, or to ask me anything, are always welcome!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What: CSS-Tricks just posted about this last Friday and since I could have used this that day (!) but didn&#8217;t find it until now, I&#8217;m adding a note here for myself: You have a page with a table of contents (with links), or you have a one page website with in-page navigation and you have &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"scroll-margin-top\" class=\"read-more button\" href=\"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2020\/scroll-margin-top\/#more-925\" aria-label=\"Read more about scroll-margin-top\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23,17,81,192,136,35],"class_list":["post-925","post","type-post","status-publish","format-standard","hentry","category-new-things","tag-can-i-use","tag-css","tag-css-tricks","tag-margin","tag-scrolling","tag-stack-overflow","infinite-scroll-item"],"_links":{"self":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/comments?post=925"}],"version-history":[{"count":3,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":1071,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/925\/revisions\/1071"}],"wp:attachment":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}