<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sticky Header Example</title> <style> body { margin: 0; font-family: Arial, sans-serif; } header { background-color: #333; color: #fff; padding: 10px; text-align: center; position: fixed; top: 0; width: 100%; z-index: 1000; } /* Add some space to the content to prevent it from being hidden under the fixed header */ body { padding-top: 60px; } section { padding: 20px; } </style> </head> <body> <header> <h1>Sticky Header</h1> </header> <section> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...</p> <!-- Add more content here --> </section> <script> // JavaScript to add a class when scrolling down, and remove it when scrolling back to the top window.addEventListener('scroll', function() { var header = document.querySelector('header'); var scrollPosition = window.scrollY; if (scrollPosition > 50) { header.classList.add('scrolled'); } else { header.classList.remove('scrolled'); } }); </script> </body> </html>