The Wayback Machine - https://web.archive.org/web/20240914130748/https://www.geeksforgeeks.org/how-to-create-sticky-header-footer-on-a-web-page-using-html-css-and-javascript/
Open In App

How to Create Sticky Header/Footer on a Web Page using HTML CSS and JavaScript?

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 properties

  • Position: sticky; is a CSS property that enables an element to transition between relative and fixed positioning depending on the user’s scroll position. This property is commonly used for sticky headers or navigation menus, ensuring their visibility and accessibility throughout the page scroll, thus enhancing the user experience with consistent navigation.
  • Position: fixed; is a CSS property that positions an element relative to the browser window (viewport) and keeps it fixed in that position while scrolling. It removes the element from the normal document flow, ensuring it remains unaffected by scrolling or layout changes. The precise location of the fixed element within the viewport is determined using the `top`, `bottom`, `left`, or `right` properties.

Syntax: To make a sticky header, CSS position, and the top property can be used.

.header {
position: sticky;
}

Sticky footers are like sticky headers, but with differences.

.footer {
position: fixed;
}

Approach 1: Using CSS

This HTML and CSS code constructs a web page with a sticky header and fixed footer. The CSS styles establish the layout: a sticky header with a navigation menu and a fixed footer displaying copyright text. The content area has a bottom margin to accommodate the sticky footer. The header remains at the top, while the footer is fixed at the bottom. This basic approach focuses on CSS for layout and positioning. To achieve dynamic footer sizing, JavaScript could be introduced to adapt the footer’s behavior based on content height and window size.

Example: In this example, we are using the above-explained approach.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        Create Sticky Header/Footer on a Web Page
        using HTML CSS and JavaScript
    </title>

    <style>
        body {
            margin: 0px;
            padding: 0px;
            box-sizing: border-box;
        }

        .header {
            position: sticky;
            top: 0;
            text-align: center;
            background-color: lightgray;
            padding: 20px;
        }

        .navigation ul {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }

        .navigation li {
            display: inline;
            margin-right: 10px;
        }

        .navigation a {
            text-decoration: none;
            color: #333;
        }

        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            background-color: lightgray;
            padding: 10px;
            text-align: center;
        }

        .content {
            margin-bottom: 100px;
        }
    </style>
</head>

<body>
    <header class="header">
        <h1>Sticky Header</h1>
        <nav class="navigation">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#html">HTML</a></li>
                <li><a href="#css">CSS</a></li>
                <li><a href="#js">JavaScript</a></li>
            </ul>
        </nav>
    </header>

    <div class="content">
        <h2 style="text-align: center;" id="html">
            HTML
        </h2>

        <p>
            HTML stands for HyperText Markup Language. 
            It is used to design the web pages. With 
            the help of HTML, you can create a complete 
            website structure. HTML is the combination 
            of Hypertext and Markup language. Hypertext
            defines the link between the web pages and 
            markup language defines the text document 
            within the tag that define the structure 
            of web pages.
        </p>

        <h2 style="text-align: center;" id="css">
            CSS
        </h2>
        <p>
            CSS (Cascading Style Sheets) is used to 
            styles web pages. Cascading Style Sheets 
            are fondly referred to as CSS. The reason 
            for using this is to simplify the process 
            of making web pages presentable. It allows 
            you to apply styles on web pages. More 
            importantly, it enables you to do this 
            independently of the HTML that makes up 
            each web page.
        </p>

        <h2 style="text-align: center;" id="js">
            JavaScript
        </h2>        
        <p>
            JavaScript is a lightweight, cross-platform, 
            single-threaded, and interpreted compiled 
            programming language. It is also known as 
            the scripting language for webpages. It is 
            well-known for the development of web pages, 
            and many non-browser environments also use it.
            JavaScript is a weakly typed language (
            dynamically typed). JavaScript can be used 
            for Client-side developments as well as 
            Server-side developments. JavaScript is both 
            an imperative and declarative type of language. 
            JavaScript contains a standard library of 
            objects, like Array, Date, and Math, and a 
            core set of language elements like operators, 
            control structures, and statements. 
        </p>
    </div>

    <footer class="footer">
        <p>© 2023 GFG. All rights reserved.</p>
    </footer>
</body>

</html>

Output:

Approach 2: Using JavaScript

The HTML structure­ consists of a header featuring navigation links, a main conte­nt area section encompassing substantial te­xt, and a footer that showcases a copyright notice. The CSS file­ contains definitions for the layout of a we­bpage. It specifies the­ visual appearance of key e­lements such as the he­ader, footer, and content se­ction. These style rule­s contribute to creating an appealing de­sign for the website. JavaScript utilizes modularization with two functions. One of these­ functions, known as toggleStickyClass, is responsible for managing the­ addition or removal of the ‘sticky’ class for ele­ments based on both the scroll position and an offse­t value provided. The othe­r function, handleScroll, ensures the prope­r behavior of sticky eleme­nts by invoking toggleStickyClass for both header and foote­r elements whe­n users scroll.

Syntax:

header.classList.toggle('sticky', isPastHeader);   
footer.classList.toggle('sticky', isPastFooter);

Example: In this example, we are using the above-explained approach.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Using JavaScript</title>
</head>
<style>
    body {
        margin: 0;
        font-family: Arial, sans-serif;
    }

    header,
    footer {
        background-color: #333;
        color: #fff;
        padding: 10px 0;
        text-align: center;
    }

    nav ul {
        list-style: none;
        padding: 0;
    }

    nav li {
        display: inline-block;
        margin-right: 20px;
    }

    nav a {
        text-decoration: none;
        color: #fff;
    }

    nav a:hover {
        color: red;
    }

    main {
        padding: 20px;
    }

    section {
        background-color: #e6e6e6;
        padding: 20px;
        border-radius: 5px;
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
    }

    footer {
        position: fixed;
        bottom: 0;
        width: 100%;
    }

    .sticky {
        position: sticky;
        top: 0;
        z-index: 100;
    }

    p {
        text-align: center;
    }
</style>

<body>
    <header class="sticky">
        <nav>
            <ul>
                <li><a href="#html">HTML</a></li>
                <li><a href="#css">CSS</a></li>
                <li><a href="#Js">JavaScript</a></li>

            </ul>
        </nav>
    </header>

    <main>
        <section>
            <h1 style="color: green; text-align: center;" 
                id="html">
                HTML
            </h1>
            <p>
                HTML stands for HyperText Markup Language.
                It is used to design the web pages. With
                the help of HTML, you can create a complete
                website structure. HTML is the combination
                of Hypertext and Markup language. Hypertext
                defines the link between the web pages and
                markup language defines the text document
                within the tag that define the structure
                of web pages.
            </p>
            <h1 style="color: green; text-align: center;" 
                id="css">
                CSS
            </h1>
            <p>
                CSS (Cascading Style Sheets) is used to
                styles web pages. Cascading Style Sheets
                are fondly referred to as CSS. The reason
                for using this is to simplify the process
                of making web pages presentable. It allows
                you to apply styles on web pages. More
                importantly, it enables you to do this
                independently of the HTML that makes up
                each web page.
            </p>
            <h1 style="color: green; text-align: center;" 
                id="Js">
                JavaScript
            </h1>
            <p>
                JavaScript is a lightweight, cross-platform,
                single-threaded, and interpreted compiled
                programming language. It is also known as
                the scripting language for webpages. It is
                well-known for the development of web pages,
                and many non-browser environments also use it.
                JavaScript is a weakly typed language (
                dynamically typed). JavaScript can be used
                for Client-side developments as well as
                Server-side developments. JavaScript is both
                an imperative and declarative type of language.
                JavaScript contains a standard library of
                objects, like Array, Date, and Math, and a
                core set of language elements like operators,
                control structures, and statements.
            </p>
        </section>
    </main>

    <footer class="sticky">
        <p>&copy; 2023 Geeksforgeeks. All rights reserved.</p>
    </footer>

    <script> const header = document.querySelector('header');
        const footer = document.querySelector('footer');
        const headerOffset = header.offsetTop;
        const footerOffset = footer.offsetTop;

        function handleScroll() {
            if (window.pageYOffset >= headerOffset) {
                header.classList.add('sticky');
            } else {
                header.classList.remove('sticky');
            }

            if (window.pageYOffset >= footerOffset - window.innerHeight) {
                footer.classList.add('sticky');
            } else {
                footer.classList.remove('sticky');
            }
        }

        window.addEventListener('scroll', handleScroll);
    </script>
</body>

</html>

Output:



Similar Reads

How to make a Sidebar Sticky on Page Scroll using CSS ?
A Sticky Sidebar contains important links and navigation options fixed on the screen, making it easy for users to access them while scrolling down. This improves the user experience by saving them from scrolling back to the top of the page to find essential information. We have used two different ways to achieve this effect including, position: fix
4 min read
Create a Sticky Social Media Bar using HTML and CSS
Build a Sticky Social Media Bar with HTML and CSS, integrating Font Awesome icons for stylish and functional social links. Utilize CSS positioning to ensure the bar stays visible while users scroll, enhancing website engagement and accessibility. ApproachCreate a basic site structure of an HTML file and also attach the CDN link of the Font-Awesome
3 min read
How to Create a Fixed/Sticky Header on Scroll with CSS and JavaScript?
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
3 min read
How to create fixed/sticky footer on the bottom using Tailwind CSS ?
In this article, we are going to create a fixed/sticky footer on the bottom using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. With Tailwind CSS we can create a design by simply adding classes. Installation: Method 1: Install Tailwind via npm Step 1: npm
2 min read
How to create a sticky image using CSS ?
A sticky image in CSS refers to an image that remains fixed within the viewport when scrolling, but only after it crosses a specified position. This effect is achieved using the `position: sticky;` property, combined with a specific top, right, bottom, or left value. For giving a sticky image effect to the web page two approaches can be used these
5 min read
How to Create a Sticky Footer Using CSS Flexbox?
Creating a sticky footer using CSS Flexbox ensures that the footer remains at the bottom of the viewport or the content and provide a consistent layout experience. In this article we will cover approach to create a sticky footer using CSS Flexbox. ApproachSet the html and body elements to 100% height and remove default margins.Style the body to be
3 min read
How to Create a Sticky Element using CSS?
A sticky element is a positioning technique in CSS that allows an element to behave like a relatively positioned element until a specific scroll position is met, after which it behaves like a fixed element. This is particularly useful for creating sticky headers, sidebars, or any other UI elements that need to remain visible while scrolling. There
3 min read
How to Create A Sticky NavBar Using Tailwind CSS?
Sticky Navbar in Tailwind CSS is mostly used in applications where maintaining quick access to navigation links is essential as users scroll through content. It ensures that the navbar remains visible at the top of the screen, enhancing user experience by providing constant access to key sections of the website. This feature is particularly useful
4 min read
How to Create Multicolored Text in a Web Page using HTML and CSS?
Creating Multicolored Text on a Web Page involves using various HTML and CSS techniques to apply different colors to specific parts of the text. This can be achieved using the deprecated &lt;font&gt; element for basic color applications or more advanced methods like CSS linear gradients for modern and visually appealing effects. Table of Content Us
3 min read
How to set sticky navbar only for first section of page?
Earlier Bootstrap gave the option to set navbar fixed for certain sections and absolute for others in Affix jQuery plugin but the new Bootstrap 4.0 dropped this option and recommended to use javascript for that. We can use Javascript to set the navbar as sticky for the first section and absolute for the rest section. For doing that you just have to
4 min read
How to create a Landing page using HTML CSS and JavaScript ?
A landing page, also referred to as a lead capture page, static page, or destination page, serves a specific purpose and typically appears as a result of online advertising or search engine optimization efforts. Unlike a homepage, a landing page is stripped of distractions and focuses on capturing visitor information through a form, while reassurin
7 min read
Create a Bookmark Landing Page using HTML CSS and JavaScript
In this article, we are going to implement a Bookmark Landing Page using HTML, CSS, and JavaScript. Users can effortlessly add, manage, and remove bookmarks, resulting in a tidy digital library for their favorite websites. Bookmarking the Landing Page refers to a web page or website where the users bookmark the website, in order to save it in their
5 min read
Design a web page using HTML and CSS
Create a webpage with HTML and CSS. Use HTML to structure content, incorporating elements like headings, paragraphs, and images. Apply CSS for styling, including colors, fonts, margins, and positioning. Ensure responsiveness for various devices using media queries and flexible layouts. Preview:Approach:Create a basic HTML structure with a navigatio
4 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 of the current content. Preview Image: Approach:We'
2 min read
How to create Twitter Login Page using HTML CSS JavaScript ?
In this article, we will see How To Make a Twitter Login Page Like Using HTML CSS JavaScript. The code produces an attractive login page with a design similar to the Twitter login interface. The JavaScript incorporates straightforward validation for the email input field upon submitting the form. Preview ApproachCreate a div, button, input text, an
4 min read
Create a Single Page Application using HTML CSS & JavaScript
In this article, we are going to design and build a cool and user-friendly single-page application (SPA) using just HTML, CSS, and JavaScript. A single-page application contains multiple pages which can be navigated or visited without loading the page every time. This makes things faster and more interesting. Preview Image: Approach:Firstly, we wil
4 min read
How to Create a Sticky and Split Footer in Bootstrap ?
A sticky footer is a layout element used in web design that maintains a web page's footer fixed at the bottom of the viewport, no matter the content's height. This design feature improves the user experience and gives the website an attractive look by ensuring the footer is visible as users scroll through the page. ApproachCreate the layout of the
3 min read
What is the difference between position:sticky and position:fixed in CSS ?
In CSS, position: sticky and position: fixed both offer elements the ability to stay in a fixed position during scrolling. These two CSS properties, while seemingly similar, have distinct behaviors that can significantly impact the layout and user experience of a webpage. This article will tell you the differences, providing you more understanding
6 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 down. Glimse of Affix/sticky Navbar Creating Struc
4 min read
How to create sticky table headers in Chrome ?
The headers of a table remain fixed at the top of the screen while you scroll down through the table are called the sticky table headers. They are used to help the viewers to know the purpose of data in the columns when the original headers are lost from the view. It eliminates the inconvenience of scrolling up and down the table to get the referen
2 min read
How to create sticky footer in ReactJS ?
Creating a sticky footer in React jS is a common requirement when designing web applications or websites. A sticky footer remains fixed at the bottom of the screen, regardless of the content's height. In this article, we will see how to create a sticky footer in ReactJS. Prerequisites:NPM &amp; Node.jsReact JSCSS Positioning ElementSteps to Create
2 min read
How to Create a Sticky Footer in Bootstrap ?
In any webpage footer is the section that occurs at the end of the page. In many web pages, we can see that footers are sticky which means that whether we scroll up or down the page the footer will stay in its place which is at the bottom of the page. How to implement a sticky footer using BootstrapIn Bootstrap, there are predefined classes given a
5 min read
Foundation CSS Sticky Navigation
Foundation CSS is a free and open-source front-end framework that aids in the creation of flexible web designs. It's one of the most powerful CSS frameworks, and it works across all devices, delivering a responsive grid as well as HTML and CSS UI components, templates, and more. It also includes some optional JavaScript Extensions features. Because
5 min read
Foundation CSS Sticky Advanced
Foundation CSS is a free and open-source front-end framework for developing flexible web designs. It's one of the most powerful CSS frameworks, and it delivers a responsive grid, as well as HTML and CSS UI components, templates, and more, across all platforms. It also has several JavaScript extension features that are optional. It also renders quic
9 min read
Foundation CSS Sticky
Foundation CSS is an open-source &amp; responsive front-end framework built by the ZURB foundation in September 2011, which makes it easy to design beautiful responsive websites, apps, and emails that look amazing &amp; can be accessible to any device. The framework is built on SaaS-like bootstrap. It is more sophisticated, flexible, and easily cus
9 min read
Foundation CSS Kitchen Sink Sticky
Foundation CSS is an open-source and responsive front-end framework built by the ZURB foundation in September 2011, which makes it easy to design beautiful responsive websites, apps, and emails that look amazing and can be accessible to any device. The framework is built on SaaS-like bootstrap. It is more sophisticated, flexible, and easily customi
6 min read
Foundation CSS Off-canvas Sticky
Foundation CSS is a free, open-source front-end framework for creating dynamic web designs. It includes a responsive grid, HTML and CSS UI components, templates, and more, making it one of the most comprehensive CSS frameworks. It also has a number of JavaScript Extension features that can be turned on or off. It also renders quickly on mobile devi
4 min read
Foundation CSS Menu Sticky Navigation
Foundation CSS is an Open-source front-end framework that makes it simple and quick to build stunning, responsive websites, mobile apps, and emails. September of that year saw ZURB publish it. Many companies, including Facebook, eBay, Mozilla, Adobe, and even Disney, use it. Based on the Bootstrap framework, this framework mimics SaaS. More complex
7 min read
Foundation CSS Sticky Navigation Dropdown Menu
Foundation CSS is an open-source front-end framework that is used to build an attractive responsive website, email, or app quickly and easily. In September 2011, ZURB published it. It is used by many companies, including Facebook, eBay, Mozilla, Adobe, and even Disney. The Bootstrap framework serves as the foundation for this platform, which mimics
7 min read
Foundation CSS Top Bar Sticky Navigation
Foundation CSS is a free and open-source front-end framework for developing flexible web designs. It's one of the most powerful CSS frameworks, and it delivers a responsive grid, as well as HTML and CSS UI components, templates, and more, across all platforms. It also has several JavaScript Extension features that are optional. It also renders quic
7 min read