HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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… 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><body>
<header></header>
<main>Left column</main>
<aside>Sticky Right Column</aside>
</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…</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… 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…</li>
<li>which made me think about grid…</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>
/*
* 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;
}
"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 ) });
Also see: Tab Triggers