Build a Sticky Header and Hero Section From Scratch

Zusammenfassung

Pixel shares a practical guide to building a sticky header and responsive hero section from scratch. It explains common reasons sticky positioning fails, demonstrates a hide-on-scroll script, and covers mobile-friendly sizing with svh and clamp(), plus focus and reduced-motion considerations.

DEV Community

Build a Sticky Header and Hero Section From Scratch

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. The hero uses min-height with the svh unit, never a fixed height, and a font size built with clamp(). If you're a beginner, read it from the top and click both live demos, because seeing a header that refuses to stick teaches more than any definition. If you already build layouts every day, jump to the section on why the header doesn't stick and then to the scroll script. Those are the places where people who know the basics still lose an afternoon. The parts about focus, reduced motion and short screens are for everyone who ships to real users. Note: Tested with: Chrome 156 (headless) on 2026-11-13, at 760, 375 and 320 pixels wide. Every demo and snippet in this article was run there and the numbers I mention, like header heights and font sizes, come from those runs. Safari and Firefox were not tested for this article, so click the demos in your own browser too. Why we rebuild a header everyone has already seen This article is part of a series I call Concepts in practice. Instead of learning CSS properties one by one from a list, we take a piece of a website you already know from daily life, rebuild it, and look at the tricks behind it. You learn faster when you already know what the finished thing should look like. The header and the hero are the first two things on nearly every landing page, shop and app. They look simple. A strip with a logo and three links, then a big title. And yet they're where a lot of beginner sites quietly fall apart: the header that sticks on the laptop but not in the real project, the anchor link that lands with the heading hidden under the header, the hero that's cut off on a phone because of the address bar. By the end you'll have built a page that does four things: The header stays at the top of the screen while you scroll. It slides away when you scroll down to read, and comes back the moment you scroll up a little. It never hides the heading you jumped to or the link you're focused on with the keyboard. The first section fills the screen on a laptop and on a phone, without cutting anything off. Warning: Rebuild the pattern, never the brand. A sticky header belongs to nobody. A company's logo, wording, photos and colours do belong to someone. Everything in this article, from the bakery name to the colours, is made up. Do the same when you practise, and especially before anything you build goes on a public page. A sticky header is a normal element with one extra rule Let's get the words straight first. The header is the strip at the top of a page with the logo and the main links. The hero is the first big section under it: a headline, one or two lines of text and usually one button. And sticky is a value of the CSS position property that makes an element scroll normally until it reaches a line you choose, and then stay on that line. Here's a comparison from normal life. Think of a fridge magnet on a sheet of paper you're sliding up the fridge door. The magnet moves with the paper until it hits the top edge of the door, and then it stays there while the paper keeps sliding. But it can never leave the paper. When the bottom of the sheet arrives, the magnet goes with it. That last part matters, ...
Build a Sticky Header and Hero Section From Scratch
Powered by Readybot.io

Das Neueste von Pixel

Stop Paying for AI Renders in Test Orders

Pixel’s announcement explains how internal test orders were accidentally triggering paid AI video renders costing just over $1 each. It details a server-side dry-run guard …

Sandblasting Grit Guide: 120 vs 220 vs 320

Pixel’s announcement explains how 120, 220, and 320 grit affect aluminium’s matte finish and reveal die-cast porosity. It also covers how pressure, blast distance, and …

NVIDIA Cosmos 3 Edge: Physical AI on Jetson

Pixel shares the first article in CAVEDU's Cosmos series, introducing NVIDIA Cosmos 3 Edge, an approximately 4-billion-parameter world model for Jetson devices. It explores multimodal …

Laravel Semantic Search with OpenAI and Pinecone

Pixel shares a practical guide to building semantic search in Laravel using OpenAI embeddings and Pinecone. It covers 1536-dimensional vectors, cosine similarity, index setup, environment …