how to implement using html and scss a website header which has a height of a whole…
- User
how to implement using html and scss a website header which has a height of a whole screen but it smoothly collapses on scroll to the slim version on the top
Assistant
To implement a full-screen header that smoothly collapses into a slim version when the user scrolls, we can use HTML, SCSS, and JavaScript. Below is a clean and functional implementation that ensures a smooth transition and proper layout adjustments.
✅ Features
- Header starts at full screen height (
100vh) - Smooth collapse to a smaller height (e.g.,
60px) on scroll - Content is adjusted to avoid overlap
- Responsive and lightweight
🧩 HTML Structure
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Collapsible Header</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <header class="hero-header"> <div class="hero-content"> <h1>Welcome to My Site</h1> </div> </header> <main> <p>Scroll down to see the header collapse.</p> <div style="height: 2000px;">Scroll down...</div> </main> <script src="script.js"></script> </body> </html>
🎨 SCSS (styles.scss)
body { margin: 0; font-family: sans-serif; } .hero-header { position: fixed; top: 0; left: 0; width: 100%; height: 100vh; background: #333; color: white; transition: height 0.3s ease; &.collapsed { height: 60px; } .hero-content { display: flex; align-items: center; justify-content: center; height: 100%; text-align: center; } } main { padding-top: 100vh; transition: padding-top 0.3s ease; }
🧠 JavaScript (script.js)
const header = document.querySelector('.hero-header'); const main = document.querySelector('main'); window.addEventListener('scroll', () => { if (window.scrollY > 0) { header.classList.add('collapsed'); main.style.paddingTop = '60px'; } else { header.classList.remove('collapsed'); main.style.paddingTop = '100vh'; } });
🔍 How It Works
- Header is fixed at the top with
height: 100vh. - Main content has
padding-top: 100vhto prevent overlap. - When the user scrolls down (
window.scrollY > 0), the header collapses to60px, and the main content'spadding-topis updated to match the new height. - The
transitionon the header and main content ensures smooth animation.
💡 Optional Enhancements
- Scroll direction tracking: Collapse only when scrolling down, expand when scrolling up.
- Mobile responsiveness: Adjust the collapsed height or add media queries.
- Debouncing: Improve performance for frequent scroll events.
📦 Final Notes
- This approach uses
position: fixedfor the header, which is ideal for full-screen headers that remain visible while scrolling. - The
transitionon both the header height and main content padding ensures a smooth visual effect. - You can customize the
60pxheight to suit your design needs.
Let me know if you'd like a version with scroll direction tracking or animations! 🚀
- Header starts at full screen height (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.