Loading video player…

Keep the Header in View

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: relative and z-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: fixed would 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 relative to sticky in 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 as top, and this rule sets none. With no inset, top is auto, and there’s no line to stop at.
  • top: 0 is the line. Add top: 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, top counts 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-index of 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, use overflow-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 id of coming-up on the Coming Up heading (its words, lowercase, joined by a hyphen) and a link to #coming-up at 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-top makes room. When the browser scrolls a heading into view, it leaves that much space above it. The header is 153 pixels tall, and 10rem is 160 pixels, so with scroll-margin-top: 10rem on every h2, 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

Final Code

The end of events/layout.css:

Language: CSS Filename: 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:

Language: 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.

00:00 In the last lesson, you gave the events page’s header a z-index of 2, so it stays in front of the cards. Now scroll down through the seven events. The header scrolls away with the page.

00:12 Rosa wants the garden’s name and the nav in reach the whole time, however far down a visitor scrolls. So the header should stop at the top of the window and stay there.

00:24 The position property has a value called sticky for this. It’s not fixed, which takes the header out of the flow, so the banner slides up under it. A sticky box keeps its place in the flow.

00:36 In layout.css, change relative to sticky in the header rule, and save. At the top of the page, nothing changes. Your turn: pause the video and predict whether the header stays in view when you scroll down.

00:57 No. It 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 as top, and this rule sets none.

01:11 So the top is auto, and there’s no line to stop at.

01:17 Add that line. Under position: sticky, type top: 0, and save. Now scroll again. The header moves up with the page until its top edge reaches the window’s top.

01:29 There it stops. For a sticky box, top counts from the edge of the box that scrolls, here the window, not from a containing block like the badge’s.

01:40 The cards slide under it, and so does the Family-friendly badge. The header’s z-index of 2 keeps it in front, and its background hides what passes behind.

01:50 Scroll back up, and the header settles where it started, in the flow. Watch out: if a wrapper around the header has overflow-x set to hidden, the header sticks to that wrapper instead of the window, and scrolls away with the page.

02:05 To only cut content off, use overflow-x: clip, which never makes a box scroll.

02:13 Rosa also wants a link in the banner that jumps straight to the events. In upcoming.html, give the Coming Up heading an id of its words, lowercase, joined by a hyphen.

02:24 At the end of the banner’s paragraph, add a link to it: a hash sign, then that id. Save, and click See what’s coming up.

02:33 The browser scrolls the heading to the top of the window. But that’s where the header sits now, so the heading lands under it, out of sight. The heading needs room above it.

02:44 In layout.css, give every h2 a scroll-margin-top of 10rem in a new rule. When the browser scrolls a heading into view, it leaves that much space above it.

02:55 The header is 153 pixels tall, and 10rem is 160. Save, scroll back up to the link, and click it again. Coming Up lands below the header, in full.

03:08 So a sticky header needs position: sticky and an inset such as top: 0, the line where it stops. And the headings that links jump to need a scroll-margin-top taller than the header.

03:22 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.

03:32 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.

Become a Member to join the conversation.