A sticky header and footer stay visible on the screen when you scroll. This helps users always see important info or navigation.
Sticky header and footer in Tailwind
<header class="sticky top-0">...</header> <footer class="sticky bottom-0">...</footer>
sticky makes the element stick when scrolling.
top-0 and bottom-0 fix the element to the top or bottom edge.
<header class="sticky top-0 bg-white shadow">Header content</header><footer class="sticky bottom-0 bg-gray-200 p-4">Footer content</footer><header class="sticky top-0 z-50 bg-blue-600 text-white p-3">Sticky Header</header> <footer class="sticky bottom-0 z-50 bg-blue-600 text-white p-3">Sticky Footer</footer>
This example shows a sticky header and footer using Tailwind CSS. The header stays at the top and the footer stays at the bottom while you scroll the tall content in the middle.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Sticky Header and Footer</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex flex-col min-h-screen"> <header class="sticky top-0 bg-blue-700 text-white p-4 shadow"> <h1 class="text-lg font-bold">Sticky Header</h1> </header> <main class="flex-grow p-4"> <p>Scroll down to see the footer stay visible.</p> <div class="h-[150vh] bg-gray-100 mt-4 p-4"> <p>This is a tall content area to enable scrolling.</p> </div> </main> <footer class="sticky bottom-0 bg-blue-700 text-white p-4 shadow"> <p>Sticky Footer</p> </footer> </body> </html>
Use z-50 or higher if sticky elements get hidden behind other content.
Sticky elements keep their place in the page flow but stay visible when scrolling.
Make sure the parent container has enough height to allow scrolling.
Sticky header and footer stay visible when you scroll.
Use Tailwind classes sticky, top-0, and bottom-0 to create them.
They improve navigation and user experience on long pages.
Practice
sticky do when applied to a header or footer?Solution
Step 1: Understand the sticky class behavior
Thestickyclass makes an element stick to a position relative to the viewport when scrolling.Step 2: Apply to header or footer context
When used on a header or footer, it keeps that element visible at the top or bottom as the page scrolls.Final Answer:
It keeps the element visible at a fixed position while scrolling. -> Option CQuick Check:
Sticky = fixed visibility on scroll [OK]
- Confusing sticky with fixed positioning
- Thinking sticky hides elements on scroll
- Assuming sticky changes colors automatically
Solution
Step 1: Identify sticky footer classes
To make a footer sticky at the bottom, usestickywithbottom-0so it sticks to the bottom edge.Step 2: Compare other options
top-0sticks to top,fixedis different positioning, andabsolutedoes not stick on scroll.Final Answer:
<code>sticky bottom-0</code> -> Option AQuick Check:
Sticky + bottom-0 = sticky footer [OK]
- Using top-0 for footer instead of bottom-0
- Confusing fixed with sticky positioning
- Using absolute which doesn't stick on scroll
sticky top-0 bg-white shadow classes to a header element in Tailwind CSS?Solution
Step 1: Analyze the classes
sticky top-0makes the header stick to the top of the viewport.bg-whitesets a white background.shadowadds a subtle shadow.Step 2: Predict visual effect on scroll
The header stays visible at the top with white background and shadow as the user scrolls down the page.Final Answer:
The header sticks to the top with white background and shadow while scrolling. -> Option DQuick Check:
sticky top-0 + bg-white + shadow = sticky visible header [OK]
- Thinking sticky hides header on scroll
- Confusing bottom-0 with top-0
- Ignoring background and shadow effects
Solution
Step 1: Understand the problem
The sticky header is behind other content, meaning it has a lower stacking order (z-index).Step 2: Use z-index to fix stacking
Addingz-50increases the z-index, bringing the header above other elements.Final Answer:
z-50 -> Option BQuick Check:
Use z-50 to fix sticky header layering [OK]
- Using opacity which changes transparency
- Using hidden which hides the element
- Using overflow-hidden which clips content
Solution
Step 1: Check header and footer sticky positions
Header \nContent \n usessticky top-0for header andsticky bottom-0for footer, which is correct for sticky header and footer.Step 2: Verify main content scroll area
Main usesoverflow-autoand height calculated as full viewport minus header/footer height, allowing scrolling between sticky elements.Step 3: Compare other options
Header \nContent \n uses fixed positioning which overlays content;Header \nContent \n swaps top and bottom for header/footer incorrectly;Header \nContent \n uses absolute which does not stick on scroll.Final Answer:
Option A code snippet correctly implements sticky header and footer with scrollable main content. -> Option AQuick Check:
Sticky top-0 + sticky bottom-0 + scrollable main = correct layout [OK]
- Using fixed instead of sticky causing overlap
- Swapping top and bottom classes on header/footer
- Not setting main content height for scrolling
Ready to prove it?
Pass the test and this pattern moves unknown
