<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>RSS Feed for css - James Stone&apos;s Blog</title><description>Blog posts tagged with css</description><link>https://jamesst.one/</link><item><title>Scroll padding top</title><link>https://jamesst.one/posts/scroll-padding-top/</link><guid isPermaLink="true">https://jamesst.one/posts/scroll-padding-top/</guid><description>&lt;![CDATA[&lt;p&gt;When you have a &lt;strong&gt;sticky&lt;/strong&gt; header, clicking an anchor link can cause the target element to be hidden behind the header. TIL &lt;code&gt;scroll-padding-top&lt;/code&gt; solves this by adding an offset to the scroll position.&lt;/p&gt;
&lt;p&gt;I think a screenshot best describes the problem:&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;#x22;src&amp;#x22;:&amp;#x22;../post_assets/sticky.png&amp;#x22;,&amp;#x22;alt&amp;#x22;:&amp;#x22;Sticky Header covering linked anchor&amp;#x22;,&amp;#x22;index&amp;#x22;:0}&quot;&gt;&lt;/p&gt;
&lt;p&gt;here I have made set the header background’s opacity to 50% so you can see when you click the anchor the header obscures the target element.&lt;/p&gt;
&lt;p&gt;On this site, I use it in the &lt;code&gt;Nav.astro&lt;/code&gt; component to ensure that the header height is accounted for when scrolling to a fragment:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;css&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;:root&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  --header-height&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;rem&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  scroll-padding-top&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;--header-height&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;nav&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  height&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;--header-height&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  position&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sticky&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  top&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This ensures that when a user navigates to an anchor, the top of the content is aligned with the bottom of the sticky navigation bar rather than being obscured by it.&lt;/p&gt;
&lt;p&gt;eg:&lt;/p&gt;
&lt;p&gt;&lt;img __ASTRO_IMAGE_=&quot;{&amp;#x22;src&amp;#x22;:&amp;#x22;../post_assets/sticky-fixed.png&amp;#x22;,&amp;#x22;alt&amp;#x22;:&amp;#x22;Sticky Header not covering linked anchor&amp;#x22;,&amp;#x22;index&amp;#x22;:0}&quot;&gt;&lt;/p&gt;
&lt;p&gt;Live demo: My &lt;a href=&quot;/blog#2026&quot;&gt;2026 posts&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;More details: &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/scroll-padding-top&quot;&gt;https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/scroll-padding-top&lt;/a&gt;&lt;/p&gt;]]&gt;</description><pubDate>Wed, 28 Jan 2026 00:00:00 GMT</pubDate></item></channel></rss>