You open almost any modern website and the same two things greet you: a header with a logo and a few links that stays at the top while you scroll, and a big first section with a headline and one button. Today we build both from scratch, with our own text and colours, and I'll show you why a sticky header so often refuses to stick, how the header that hides on the way down and comes back on the way up really works, and how to make the first screen hold on a phone.
Here's the short answer first. A sticky header is a normal header with position: sticky and top: 0, placed where its parent is as tall as the page and no parent has overflow: hidden. The hide-on-scroll trick is a small script that compares the current scroll position with the last one and adds a class that slides the header up with a transform.…