How to use position: sticky In CSS

The sticky element is made to stick at the top position by the following syntax and code. that will help us to create the sticky element that will stick to the top.

Syntax:

.sticky-element {
     position: sticky;
}

Example: This example shows the use of position sticky for creating a sticky element.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title> w3wiki </title>
        <style>
            .container {
                height: 200vh; 
            }
            .sticky-element {
                position: sticky;
                top: 0; 
                background-color: #f1f1f1;
                padding: 20px;
            }
        </style>
    </head>
    <body>

        <div class="container">

            <div class="sticky-element">
                I'm a sticky element!
            </div>
            <p> 
                Introduction The day I started to work as 
                  content writer 
                for w3wiki on December 13th, 2023,
                  was a combination 
                of excitement and anticipation. The membership in 
                  a well-known platform not only lets me share my 
                  knowledge but also connect with a worldwide 
                audience of tech lovers. At that time, I did 
                opportunity would become a major turning point
                influencing my growth and development as a
                significant ways. Embracing the Opportunity 
                  with its comprehensive collection of technical 
                community, was the platform where I could make a 
                  significant contribution to it. The idea of being 
                  able to communicate my skills in subjects 
                from algorithms to programming languages 
                and humbling. I gladly took the opportunity to 
                  study the detailed technical subjects, knowing that
                  every article was a chance to get through and
                  educate the world’s readers. 
            </p>
        </div>
    </body>
</html>

Output:

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 are two main approaches to creating a sticky element using CSS:

Table of Content

  • Using position: sticky
  • Using position:fixed with JavaScript

Similar Reads

Using position: sticky

The sticky element is made to stick at the top position by the following syntax and code. that will help us to create the sticky element that will stick to the top....

Using position:fixed with JavaScript

IN this approach, we are using the position fixed so that we can fix the position of an element. The element is sticked to the fixed position of the viewport by the following syntax and code....