<!doctype html>
<html>
<head>
<title>Z-index</title>
<link href="/assets/reset.css" rel="stylesheet">
<link href="setup.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<section>
<p>This is some text in the first element.</p>
<p>There can be multiple elements within it, maybe even that wrap to multiple lines.</p>
</section>
<section>
<p>And some more in the second element.</p>
</section>
<section>
<p>Then a third one, too.</p>
</section>
<section>
<p>Let’s add a fourth.</p>
</section>
<section>
<p>Even a fifth.</p>
</section>
<section>
<p>This should let it scroll.</p>
</section>
<section>
<p>It was the best of times; it was the worst of times.</p>
</section>
<section>
<p>Scrolling is always fun.</p>
</section>
<section>
<p>I guess that isn’t true.</p>
</section>
<section>
<p>Sometimes it goes on and on, you know.</p>
</section>
<section>
<p>Okay, this is enough.</p>
</section>
<section>
<p>Let’s make a longer scroll on this one.</p>
</section>
<section>
<p>To really show off the stacking.</p>
</section>
<section>
<p>Keep going…</p>
</section>
<section>
<p>much scroll</p>
</section>
<section>
<p>Almost there!</p>
</section>
<section>
<p>One more, for good measure.</p>
</section>
</body>
</html>
setup.css
/* Same as before. */
:root { --spacing: 1rem; }
body {
font-family: sans-serif;
padding: var(--spacing);
}
section {
background-color: var(--background, deepskyblue);
border-block-start: solid black calc(var(--spacing) / 5);
padding: var(--spacing);
&:not(:first-child) { margin-block-start: var(--spacing); }
}
style.css
section:nth-child(3) {
--background: gold;
--inset: calc(3 * var(--spacing));
inline-size: calc(100% - var(--inset));
inset-block-start: var(--inset);
inset-inline-start: var(--inset);
position: sticky;
}
section:nth-child(even) { /* Every other one! */
position: relative; /* Doesn’t work for default/`static`. */
z-index: 1; /* “In front by one step.” */
}