How to Create a Fixed/Sticky Header on Scroll with CSS and JavaScript? Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report A fixed or sticky header remains at the top of the webpage when the user scrolls down. This functionality enhances navigation and user experience by keeping important links always visible. In this article, we will explore the approach to creating a fixed/sticky header on scroll using CSS and JavaScript. ApproachCreate a basic HTML structure with a header element for the fixed header and a main element to some content.Add some content inside main element using <p> tag.Style the header such as a background color, text color, padding, and width. We will set position: relative initially.Add padding to the main element and set a height to create enough scrollable content.We use JavaScript to add an event listener for the scroll event on the window object.Inside the event listener get a reference to the header element using its id.We will use window.scrollY to get the current scroll position.When the scroll position becomes greater than a specified value, we add the fixed class to the header. Otherwise we remove it.Example: Implementation to create a fixed/sticky header on scroll with CSS and JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Fixed Header</title> <style> body { margin: 0; font-family: Arial, sans-serif; } header { background-color: rgb(19, 179, 19); color: white; padding: 10px 20px; position: relative; width: 100%; transition: top 0.3s; } header.fixed { position: fixed; top: 0; z-index: 1000; } h3 { color: green; } main { padding: 20px; height: 2000px; background-color: #ccc6c6; } </style> </head> <body> <header id="header"> Fixed Header </header> <main> <h3>Welcome to GeeksForGeeks</h3> <h4>Scroll down to see the fixed header in action.</h4> <p> GeeksforGeeks (GFG) is a computer science portal founded by Sandeep Jain in 2009. It started as a blog to share computer science concepts and has grown into a comprehensive platform for learning and practicing coding. It is based in Noida, Uttar Pradesh, India. </p> <p>Industry: E-Learning, EdTech Product, Services and Operations: Provides a wide range of articles on various topics related to computer science and programming. Offers practice problems and coding competitions for users to improve their coding skills. Provides interview experiences and preparation resources for tech job aspirants. Offers online courses and tutorials on various tech topics.</p> <p> Funding Details: The company is privately owned and does not disclose its financial details. Acquisitions: No notable acquisitions. Awards and Achievements: Recognized as one of the most popular computer science portals in India.- Has a large user base of millions of users worldwide. Please note that the information provided is based on the available resources and may not be 100% accurate. </p> </main> <script> window.addEventListener('scroll', function () { const header = document.getElementById('header'); if (window.scrollY > 50) { header.classList.add('fixed'); } else { header.classList.remove('fixed'); } }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Create a Fixed/Sticky Header on Scroll with CSS and JavaScript? Y yuvraj07 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Create Shrink Header on Scroll using HTML, CSS and JavaScript ? The Shrink Navigation bar works when the user scrolls down the page. In this article, we will use HTML, CSS, and JavaScript to design a shrink navigation bar. HTML is used to create the structure, and CSS is used to set the style of the HTML structure to make it looks good. This kind of shrinking na 3 min read How to Create Sticky Header/Footer on a Web Page using HTML CSS and JavaScript? When designing a website, it's essential to consider the sticky header and footer. These elements enhance the user experience by keeping important navigation links accessible as users scroll. This article explores creating sticky headers and footers with CSS.We can do this by using the following CSS 7 min read How To Create a Table With Fixed Header and Scrollable Body? Here are the methods to create a table with a fixed header and a scrollable body.Before proceeding, enhance your understanding by exploring this comprehensive guide on HTML tables.1. Using position: sticky and overflow-yThis method employs position: sticky on the table header to keep it fixed at the 3 min read How to Get and Set Scroll Position of an Element using JavaScript ? In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript.Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties.We create an HTML div element with an id of 3 min read How to Create a Change Background on Scroll using HTML CSS and JavaScript ? In this article, we will try to change the background color of the container once the scroll occurs. If a user scrolls down the page the background color will be changed. We will use the below approach to accomplish this task.Using the scroll event on the window objectIn this approach, we will use t 6 min read How to Create Scroll Indicator using HTML CSS and JavaScript ? Scroll Indicator is a progress bar that represents how much a page has been scrolled. When we scroll down the bar fills up and when we scroll up the bar amount reduces. Approach: Now, we will create a basic webpage with text to enable scrolling and then use JavaScript to make the scroll indicator wo 3 min read How to Create a Smooth Scrolling Effect with CSS? To create a smooth scrolling effect with CSS, use the scroll-behavior: smooth property. This enables smooth transitions between sections of a webpage, offering a more seamless and polished experience for users.Instead of jumping from one section to another, smooth scrolling ensures gentle transition 6 min read Create an Infinite Scroll Page using HTML CSS & JavaScript In this article, we will create an infinite scroll page using HTML, CSS, and JavaScript. Infinite scrolling allows you to load and display content as the user scrolls down the page, providing a seamless browsing experience. We'll fetch and append new content dynamically as the user reaches the end o 2 min read How to create an Affix or sticky Navbar ? To create an affix or sticky navbar, you need to use HTML, CSS, and JavaScript. HTML will make the structure of the body, CSS will make it looks good. This kind of sticky navbar looks attractive on the website. By using JavaScript, you can easily make the navigation bar sticky when the user scrolls 4 min read How to Set Offset an HTML Anchor Element to Adjust Fixed Header in CSS ? When designing a website with a fixed header, we might face an issue where clicking on a link obscures the target element due to the header. This affects the user experience negatively as the content gets hidden. In this article, we will see how to offset HTML anchors for a fixed header, ensuring pr 5 min read Like