UNIT 24 • STAGE 6 OF 7
position: fixed, z-index, and a CSS .scrolled state class
position: fixed removes an element from normal document flow and pins it to the viewport; it stays in place as the page scrolls. You need three more properties to position it correctly:
top: 0, pins the top edge to the top of the viewportleft: 0, aligns the left edge to the left of the viewportwidth: 100%, stretches the header to the full viewport width (fixed elements do not inherit their parent's width)z-index: 1000, stacks the header on top of all other contentThe .scrolled class on the header is a CSS hook. It exists in the CSS right now, but nothing applies it yet. In Stage 7, JavaScript will add and remove this class based on how far the user has scrolled.
Add this before all other CSS, at the top of your <style> block (so it loads before any sections that might interact with it):
Also add padding-top: 160px to #hero to push the hero content down so it is not hidden behind the fixed header.
transition: background 0.3s, padding 0.3s, the header smoothly animates between its transparent and solid states. Without this, the background would snap instantly, which looks abrupt..site-header.scrolled, this CSS selector targets the header element only when it also has the scrolled class. Right now nothing adds that class; that is JavaScript's job in Stage 7.box-shadow: 0 2px 20px rgba(0,0,0,0.4), adds a soft shadow below the solid header. This gives it visual depth and separates it from the content beneath.Add the header as the very first element inside <body>, before the hero section:
Scroll in the preview; you will notice the header is there (you can see the logo and nav) but it stays transparent no matter how far you scroll. Stage 7 adds the JavaScript that detects the scroll position and toggles the .scrolled class.
You have a fixed header pinned to the top, with logo and nav links, transparent by default and ready for the scroll behavior.
Next: Stage 7, the last stage, adds the scroll event listener that makes the header transform.