×

css · level 2 · unit 5 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

keeps a box in view as you .

When a page is too long for the screen, you scroll it. A sticky box scrolls along with the page until it reaches its offset. Then it stays stuck there.

position: sticky with top: 0 makes a header that sticks to the top of the screen. Without an offset, sticky does nothing at all.

A sticky box only sticks inside its parent. When the parent scrolls away, the sticky box goes with it. Give it a background, or the words underneath show through.

<div class="header">The Daily Paw</div>
<p class="story">Scroll down: the header stays at the top while this long story slides underneath it.</p>
.header {
  position: sticky;
  top: 0;
  padding: 12px;
  border-bottom: 1px solid silver;
  background-color: white;
}
.story {
  height: 900px;
}

Press show my page to see it here.

Scroll inside the preview to watch the header stay put.