In this lesson, you kept the events page’s header in view while the page scrolls, and made sure a link that jumps to a heading doesn’t land it under that header.
Key Points
- The need. With
position: relativeandz-index: 2, the header stays in front of the cards, but it scrolls away with the page. Rosa wants the garden’s name and the nav in reach however far down a visitor scrolls. - Sticky, not fixed.
position: fixedwould take the header out of the flow, so the banner would slide up under it. A sticky box keeps its place in the flow. - Sticky on its own doesn’t stick. Change
relativetostickyin the header rule, and nothing changes at the top of the page. Scroll down, and the header scrolls away exactly as before. A sticky box behaves like a relative box until it reaches a line that you set. You set that line with an inset, such astop, and this rule sets none. With no inset,topisauto, and there’s no line to stop at. top: 0is the line. Addtop: 0, and the header moves up with the page until its top edge reaches the window’s top, and stops there. For a sticky box,topcounts from the edge of the box that scrolls, here the window, not from a containing block like the badge’s.- What passes under it. The cards and the Family-friendly badge slide under it: the header’s
z-indexof 2 keeps it in front, and its background hides what passes behind. Scroll back up, and the header settles where it started, in the flow. - Watch out for an overflow wrapper. If a wrapper around the header has
overflow-x: hidden, the header sticks to that wrapper instead of the window, and scrolls away with the page. To only cut content off, useoverflow-x: clip, which never makes a box scroll. - A jump can land under the header. Rosa wants a link in the banner that jumps straight to the events. With an
idofcoming-upon the Coming Up heading (its words, lowercase, joined by a hyphen) and a link to#coming-upat the end of the banner’s paragraph, clicking the link scrolls the heading to the top of the window, which is where the header now sits. The heading lands under it, out of sight. scroll-margin-topmakes room. When the browser scrolls a heading into view, it leaves that much space above it. The header is 153 pixels tall, and10remis 160 pixels, so withscroll-margin-top: 10remon everyh2, Coming Up lands below the header, in full. After the first jump, the link is above the window, so scroll back up to it before you click it again.
The Rule
- A sticky header needs
position: stickyand an inset such astop: 0, the line where it stops. - The headings that links jump to need a
scroll-margin-toptaller than the header.
Final Code
The end of events/layout.css:
events/layout.css
header {
position: sticky;
top: 0;
z-index: 2;
background-color: var(--page);
}
h2 {
scroll-margin-top: 10rem;
}
The events banner and the Coming Up heading in events/upcoming.html:
<div class="events-banner">
<h1>Upcoming Events</h1>
<p>Everyone's welcome, with or without a plot. <a href="#coming-up">See what's coming up</a>.</p>
</div>
<section>
<h2 id="coming-up">Coming Up</h2>
Next is Exercise: Pin the Badges and the Header, where you’ll pin a badge to a card and keep a header in view on an events page. It’s the course’s first exercise. You write the CSS right in your browser, then click Run Tests, and automated tests check it. Run them early and often, and read each failing test for clues.