Web development
Make a sidebar sticky within its layout
Use position sticky with a top offset for a sidebar that follows scrolling within its containing layout. Unlike fixed positioning, it remains tied to its normal layout and containing block.
Worked example
HTML example · save as example.html and open in a browser
<style>
.layout{display:grid;grid-template-columns:12rem minmax(0,1fr);gap:2rem}
aside{position:sticky;top:1rem;align-self:start}
main{min-height:150vh}
@media(max-width:650px){.layout{display:block}aside{position:static}}
</style>
<div class="layout"><aside><a href="#last">Last section</a></aside><main><h1>Guide</h1><p>Scroll this example.</p><h2 id="last" style="margin-top:100vh">Last section</h2></main></div>How it works
The top offset defines where the sidebar sticks. Align-self start prevents grid stretching from giving it an unhelpful full-column height. The mobile rule returns it to normal flow when two columns would crowd the article.
Limits and mistakes
Ancestor overflow settings can change which scrolling container governs sticky behavior. A sidebar taller than the viewport can also become difficult to use. Sticky elements must not obscure focused controls or hide article content at zoomed sizes.
Check the result
Scroll the page at desktop width and then test the narrow layout. Inspect ancestor overflow if the sidebar never sticks, and verify the entire sidebar remains reachable at high zoom.
Sources
Reference documentation for the behavior used in this guide: