Open In App

How to make div fixed after scroll to that div ?

In this article, we will see how to make a div fixed after you scroll to it by using the position property with a sticky value. This effect is generally used on websites when an important message needs to be shown. Set the property position to sticky to create elements that behave as fixed positions whenever the user scrolls to that position.

Fixing the Div after scrolling using position: sticky

Example 1: The below code is for designing a page where the div is fixed on scrolling to it.






<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <style>
        .container {
            height: 1500px;
        }
  
        .sticky {
            position: sticky;
            top: 20px;
            background-color: rgb(32, 165, 20);
            color: white;
            padding: 10px;
        }
    </style>
</head>
  
<body>
    <div class="container">
        <p>Please Scroll down</p>
        <div class="sticky">
            <p>This div will be fixed after scrolling to it</p>
        </div>
        <p>
            Web Design is a field related to designing
            websites on the Internet. Without a good design,
              a website fails to perform well and cannot produce
            a good impact on the user. Design is a creative 
              process that affects the ranking of the brand. 
            A good website design helps to create an engaging 
            online presence that captivates the audience. Each 
            distinct day gives you an opportunity to learn,
            progress, and do the things that push you toward 
            your goals or that make you satisfied at the end of
            the day. Web Design helps to make your web pages stand
            out in terms of design and user experience. It
            integrates technical and creative skills, by which
            you can build and maintain web pages very easily.
        </p>
    </div>
</body>
  
</html>

Output:



Fixing the Div after scrolling using position: fixed

Example 2: The below code is for designing a page where the div is fixed on scrolling to it.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <style>
        .container {
            height: 1500px;
        }
  
        .sticky {
            position: fixed;
            top: 20px;
            width: 100vw;
            height: 25px;
            font-weight: 700;
            background-color: rgb(155, 204, 32);
            color: white;
            padding-bottom: 20px;
        }
    </style>
</head>
  
<body>
    <div class="container">
        <p>Please Scroll down</p>
        <div class="sticky">
            <p>This div will be fixed
                after scrolling to it
            </p>
        </div>
        <p>
            Web Design is a field related to designing
            websites on the Internet. Without a good
            design, a website fails to perform well
            and cannot produce a good impact on the
            user. Design is a creative process that
            affects the ranking of the brand. A good
            website design helps to create an engaging
            online presence that captivates the audience.
            Each distinct day gives you an opportunity
            to learn, progress, and do the things that
            push you toward your goals or that make you
            satisfied at the end of the day. Web Design
            helps to make your web pages stand out in
            terms of design and user experience.
            It integrates technical and creative skills,
            by which you can build and maintain web pages
            very easily.
        </p>
    </div>
</body>
  
</html>

Output:


Article Tags :