Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
	<h1>Troubleshooting a CSS Grid child element with <code>position: sticky</code></h1>
	<p>By <a href="https://mrwweb.com">Mark Root-Wiley</a>, September 24, 2021</p>
</header>
<main>
	<p class="note"><em>This post has a responsive layout. It's best read at a browser width of at least <code>48em</code> / <code>768px</code>.</em></p>
	<p>This is techncially a post about using CSS sticky positioning on grid children. This is actually just a story of an average day banging my head against the keyboard until I learned to see a problem in a different way, solve it with one line of CSS, and live happily every after&hellip; until at least tomorrow when I run into some new problem.</p>
	<p>It all started when I had a cool idea for my new resume (coming soon!) that involved a sticky sidebar with a list of my skills and some fun 🪄highlighty magic🪄 as you scrolled down the page.</p>
	<p>I was pretty familiar with the <a href="https://css-tricks.com/more-like-position-tricky/">many gotchas of <code>position: sticky</code></a>, and so I thought, "Hey, I've totally got a handle on all this fancy CSS stuff. Modern CSS makes layouts quick and easy!"</p>
	<p>The layout I wanted was basically the same as this demo: multiple elements, directly in the body, laid out with CSS Grid. Here, I'll save you from opening your dev tools:</p>
	<pre><code>&lt;body>
	&lt;header>&lt;/header>
	&lt;main>Left column&lt;/main>
	&lt;aside>Sticky Right Column&lt;/aside>
&lt;/body></code></pre>
	<p>The <code>body</code> uses <code>display: grid</code> to make a simple <code>2fr</code>/<code>1fr</code> sidebar layout and the <code>aside</code> has <code>position: sticky</code> and <code>top: 2em</code>.</p>
	<blockquote>
		<p>I'll be done in no time. Doo do doo do doo.</p>
		<p><em>[Writes two CSS rules]</em></p>
		<cite>Me, Being Predictably Naive</cite>
	</blockquote>
	<p>Half an hour later, I hadn't written any more code and was questioning everything. The only thing the sidebar was sticking to was the top of the page.</p>
	<p>I re-re-re-reviewed all the <a href="https://css-tricks.com/dealing-with-overflow-and-position-sticky/">things about sticky and overflow</a> and learned to think about <a href="https://elad.medium.com/css-position-sticky-how-it-really-works-54cd01dc2d46">sticky containers</a>. I read a lot about sticky positioning not working, but everything I read still didn't seem apply to my situation.</p>
	<p>In fact, it's the situation you find yourself in at this very moment. 🤨 Right now. On your screen. There is a sidebar that has had <code>position: sticky;</code> this whole time! 😱</p>
	<h2>See? I told you it didn't work!</h2>
	<p>Really. Keep scrolling. I'll wait&hellip;</p>
	<br><br><br><br><br><br><br>
	<p>The sidebar definitely isn't sticking, is it?</p>
	<p>At this point, I was really convinced that it <em>should</em> be working, but it wasn't. I was losing hope. This was probably some super technical edge-case-y thing that I just couldn't wrap my head around. Maybe I wasn't really a good front-end developer after all. <em>Good thing I'm editing my resume, so I can just remove "CSS" from the list of skills in the sidebar. It's for the best that it won't stick so my skills will just scroll out of view.</em> 😭</p>
	<p>But then! What's that? 😲 I noticed that for some odd reason, the sidebar stuck just a little bit. I can't even replicate that now, so I really don't know what I was seeing. (There's always more to learn!) What's important is that I kept searching. Almost immediately, I found a StackOverflow post&hellip; with no answers. I don't even think I had the same problem as the person who posted.</p>
	<p>One would not think this was the ⚡AHA! moment⚡, and yet:</p>
	<ol>
		<li>the post mentioned heights&hellip;</li>
		<li>which made me think about grid&hellip;</li>
		<li>which suddenly gave me the answer!</li>
	</ol>
	<p>Have you figured it out yet? Let me give you a hint:</p>
	<div class="toggle-wrapper">
		<input type="checkbox" id="outline"><label for="outline">Outline the Sidebar!</label>
	</div>
	<h2>The Solution</h2>
	<p>Ok, fine. I'll break it down for you.
	<p>Now that you've outlined the <code>aside</code> element, you can see that it's the same height as the <code>main</code> element containing all this ranty text!</p>
	<p>That's the "problem". Because as far as the browser is concerned, we <em>can</em> see the sidebar, it's just empty once you're this far down the page. Since it's always visible and within the viewport, the sticky part of <code>position: sticky</code> never kicks in.</p>
	<p>Why is this happening? The height of the <code>aside</code> is the expected behavior of a grid child element. With CSS grid layout, the default value for <code>align-items</code> is <code>stretch</code> (unless the element has an intrinsic size).</p>
	<p>So the solution is a single line of code. The sidebar needs <code>align-self: start</code>, so that its height doesn't stretch to match its parent's height and is only as tall as its content.</p>
	<p>Ready to fix it now? Go ahead, I'll let you do the honors 🏆:</p>
	<div class="toggle-wrapper">
		<input type="checkbox" id="fix"><label for="fix">Let there be stick!</label>
	</div>
	<p>It probably took me a good hour to go from 2 lines of broken CSS to 3 lines of working CSS, but now it works and it feels so good! And more importantly, I proved to myself that I really can do this whole website code-y thing after all. I'm sure you can too. Just keep working at it.</p>
</main>
<aside id="aside" aria-live="polite" aria-atomic="true" aria-relevant="text">
	<p>Don't mind me, I'm just a boring, normal, sidebar.</p>
	<p>🎉 You fixed it!<br>⭐ Gold star for you!</p>		
</aside>
              
            
!

CSS

              
                /*
 * The Layout on medium-size screens and up
 */
@media (min-width: 48em) {
	body {
		display: grid;
		grid-template-columns: 2fr 1fr;
		grid-gap: 2em 5vw;
	}
	
	header {
		grid-column: 1 / -1;
	}

	aside {
		position: sticky;
		top: 2em;
	}
}

/*
 * Just making things prettier to look at below this
 */
* {
	margin: 0;
	box-sizing: border-box;
}

:focus {
	outline: 2px dotted Teal;
}
:focus:not(:focus-within) {
	outline: 0;
}

body {
	width: 90%;
	max-width: 100ch;
	margin: 5em auto;
	line-height: 1.5;
	font-size: 1.125rem;
	font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Ubuntu", "Roboto", "Noto Sans", "Droid Sans", sans-serif;
}

main, aside {
	margin-top: 2em;
}
@media (min-width: 48em) {
	main, aside {
		margin-top: 0;
	}
}

main * + * {
	margin-top: 1.5em;
}

header p {
	text-align: right;
	font-size: .875rem;
	color: DarkSlateGray;
}

header a {
	color: inherit;
}

a {
	color: Teal;
}
a:hover {
	text-decoration: none;
}

li {
	margin-top: .25em;
}

blockquote {
	padding-left: 1em;
	border-left: .25rem solid Teal;
}

cite {
	display: block;
	text-align: right;
	font-size: .875rem;
}
cite::before {
	content: '— '
}

main pre,
main code {
	background-color: PapayaWhip;
	padding: 0 .25em;
	border-radius: .1875rem;
	font-size: .9375rem;
	font-family: ui-monospace, "Cascadia Mono", "Segoe UI Mono", "Ubuntu Mono", "Roboto Mono", Menlo, Monaco, Consolas, monospace;
}

.toggle-wrapper {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 1em;
	border: .125rem solid Teal;
	font-size: 1.5rem;
}

input {
	accent-color: Teal;
	width: 1.5rem;
	height: 1.5rem;
}

label {
	margin: 0 0 0 .5em;
	cursor: pointer;
}

.note {
	background: WhiteSmoke;
	padding: 1em;
}

.outline {
	outline: 2px dashed Teal;
}

aside p:last-child {
	display: none;
}
.fix p:first-child {
	display: none;
}
.fix p:last-child {
	display: block;
}







/*
 * ⚠ SPOILER ALERT ⚠ Fix below.
 */














.fix {
	align-self: start;
}
              
            
!

JS

              
                "use strict"

const toggleBodyClass = (e) => {
	const checkbox = e.currentTarget;
	document.getElementById('aside').classList.toggle(checkbox.id);
}

const checkboxes = document.querySelectorAll('input[type="checkbox"]');
console.log(checkboxes);
checkboxes.forEach( (cb) =>{ cb.addEventListener( 'click', toggleBodyClass ) });
              
            
!
999px

Console