The Wayback Machine - https://web.archive.org/web/20240906231619/https://www.geeksforgeeks.org/what-is-the-difference-between-positionsticky-and-positionfixed-in-css/
Open In App

What is the difference between position:sticky and position:fixed in CSS ?

Last Updated : 06 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 of CSS positioning. First, lets see the description of each position values we have then we will start the difference between position sticky and fixed with example and explanation.

Position ValueDescription
FixedElement is positioned relative to the viewport, staying in the same place even when the page is scrolled.
StaticThe element is positioned according to the normal flow of the page, with no special positioning applied.
RelativeThe element remains in the normal flow of the document but can be moved left, right, top, or bottom relative to its normal position.
AbsoluteElement is positioned relative to the closest positioned ancestor, ignoring the normal flow of the document.
StickyElement with position: sticky and top: 0 behaves like a hybrid between fixed and relative positioning, initially positioned as static but becomes fixed when scrolled to a specific point

We will discuss only the position: fixed and sticky properties. Both of these are used to fix the element to a certain position in the HTML page. Please refer to the CSS Positioning Elements article for more details.

The position: fixed means fixed to the viewport. We provide the position values (top, bottom, right, or left) and the element stays there when the user is scrolling. No matter what is happening on screen the fixed element will not move at all.

Syntax:

selector {
position: fixed;
}

Example: When we use the position: fixed property, the element stays fixed to its position irrespective of what is happening on screen it is fixed at the viewport.

HTML
<html>

<head>
    <style>
        body {
            margin: 0;
            padding: 20px;
            font-family: sans-serif;
            background: #efefef;
        }

        .fixed {
            position: fixed;
            background: #088523;
            color: #ffffff;
            padding: 30px;
            top: 250;
            left: 10;
        }

        span {
            padding: 5px;
            border: 1px #ffffff dotted;
        }
    </style>
</head>

<body>
    <div class="fixed">This div has
        <span>position: fixed;</span>
    </div>
    <h1>
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
    </h1>
</body>

</html>

Output:

The position: sticky means the element will scroll until it reaches the offset value given to it by the user and then stays in its position. Sticky element always stays within its parent block and as soon as the parent block leaves the screen as an effect of scrolling, sticky elements also leave with it.

Syntax:

selector {
position: sticky;
}

Example: When we use the position: sticky property, the element scrolls till it touches the top, will be fixed at that place in spite of further scrolling.

HTML
<html>

<head>
    <style>
        body {
            margin: 0;
            padding: 20px;
            font-family: sans-serif;
            background: #efefef;
        }

        .sticky {
            position: sticky;
            background: #088523;
            color: #ffffff;
            padding: 30px;
            top: 0;
            left: 10;
        }

        span {
            padding: 5px;
            border: 1px #ffffff dotted;
        }
    </style>
</head>

<body>
    <h1>
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <div class="sticky">This div has
            <span>position: sticky;</span>
        </div>
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
        <br />
        <br />
        What We Offer We provide a variety of
        services for you to learn, thrive and
        also have fun! Free Tutorials, Millions
        of Articles, Live, Online and Classroom
        Courses, Frequent Coding Competitions,
        Webinars by Industry Experts, Internship
        opportunities and Job Opportunities.
    </h1>
</body>

</html>

Output:

Difference between position: fixed and position: sticky property:

S.No.

Position: fixed

Position: Sticky

1.

Element with position: fixed property is fixed to the viewport and doesn’t move irrespective of scrolling.

Element with position: sticky property can scroll to an offset value provided by the user. 

2.

Element with position: fixed property never leaves the viewport position it was fixed to.

Element with position: sticky property leaves the viewport when its parent element scrolls off the viewport.

3.

This property is supported by all the browsers.

This property is only supported by all modern browsers.

4. Element with position: fixed property does not effect the other element’s flow on the page ie it does not capture additional space.Element with position: sticky property does effect the other element’s flow in the page ie., it will take the additional space.

Mastering the use of position:sticky and position:fixed in CSS is a crucial skill for any web developer or designer. These properties allow for dynamic and engaging webpage layouts that can significantly enhance the user experience. By understanding their differences and appropriate use cases, you can create more interactive and visually appealing websites.

Supported browsers:

position: fixed is supported by:

position: sticky is supported by:



Similar Reads

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
Difference between relative , absolute and fixed position in CSS
CSS positioning is a fundamental concept in web design and development. It allows us to control the layout of elements on a webpage. This article will tell you the differences between relative, absolute, and fixed positioning in CSS. 1. Relative PositioningRelative Position: Relative positioning is a CSS technique that allows an element to be adjus
2 min read
Bootstrap 5 Position Sticky Top
Bootstrap 5 position-sticky class fixes an element at the top of the viewport when scrolling down. If not fully supported, alternatives like `position: fixed` or JavaScript solutions can mimic the sticky effect. Position Sticky Top Class: sticky-top: This class is used to set the position of the element to the sticky top of the viewport when users
2 min read
Bootstrap 5 Position Responsive Sticky Top
Bootstrap 5 Position Sticky top is used to set the element's position to the sticky top of the viewport when the user scrolls down. The Position Responsive Sticky Top feature allows adding responsiveness to the webpage. We can use some CSS property to display the effect of position fixed bottom. Position Responsive Sticky Top: sticky-top: This clas
2 min read
How to center an element using position:fixed in CSS ?
When designing web pages, achieving proper centering of elements is crucial for aesthetics and user experience. In this guide, we’ll see how to center an element using position fixed property in CSS. Whether you’re creating a landing page, a form, or any other web component, understanding these methods will enhance your design skills. Let's first c
3 min read
How to Position a Fixed Element in the Top Right Corner using CSS ?
In CSS, Positioning the fixed element in the top fight corner consists of maintaining the fixed position of the element by using Absolute Positioning or by using Flexbox Layout. These are the following methods: Table of Content Using Absolute PositioningUsing FlexboxUsing Absolute PositioningIn this approach, we are using absolute positioning to fi
2 min read
How to set fixed position but relative to container in CSS ?
In this article, we are going to learn about the fixed position but relative to the container in CSS, basically, we have two common methods, position: sticky property and position: fixed property. To set the position of an element to fixed but relative to its container, we can use the combination of the "position: fixed" and "position: relative" pr
4 min read
Difference Between Fixed and Auto Table Layout in CSS
In this article, we will see the difference between fixed auto layout vs auto table layout in CSS. Table layout in CSS is handled by the table-layout property. This property specifies the algorithm that the browser uses to determine the width of the table and its columns. There are 2 types of table layout: fixed: Table and column widths are set by
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 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 propertiesPosition: sticky; is a CSS property th
7 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
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 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
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
Bootstrap 5 Position Fixed bottom
Bootstrap 5 Position Fixed bottom is used to set the position of the element to the bottom of the viewport. We can use some CSS property to display the effect of position fixed bottom. Position Fixed Bottom Class: fixed-bottom: This class is used to set the position of the element to the bottom of the viewport. Syntax: &lt;div class="fixed-bottom"
2 min read
Bootstrap 5 Position Fixed top
Bootstrap 5 Position Fixed top is used to set the position of the element to the top of the viewport. We can use some CSS property to display the effect of position fixed top. Position Fixed Top Class: fixed-top: This class is used to set the position of the element to the top of the viewport. Syntax: &lt;div class="fixed-top"&gt; Content &lt;/div
2 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
How to set Bootstrap 4 scroll to the bottom of long sticky sidebar?
In Bootstrap 3, to set bootstrap scroll to the bottom of long sticky sidebar is possible using Affix which is handled by "Affix jQuery plugin". Unfortunately, in Bootstrap 4 migration "Affix jQuery plugin" was dropped. To achieve this affix property as like as Bootstrap 3, Bootstrap 4 recommends us CSS property i.e., position: sticky; directly in s
3 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