The Wayback Machine - https://web.archive.org/web/20240909121427/https://www.geeksforgeeks.org/how-to-make-div-fixed-after-scroll-to-that-div/
Open In App

How to make div fixed after scroll to that div ?

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

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

  • In the HTML file create a div that you want to make fixed on scroll.
  • Apply the sticky property to a div in the CSS file.
  • Here, the property position is set to the sticky to the specified element making the div named “sticky” fixed after you scroll to that div.
  • Apply the position using top, bottom, left, or right properties according to position where it should be fixed.

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

HTML
<!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

  • In the HTML file create a div that you want to make fixed on scroll.
  • For Defining the styles for the sticky div element named “sticky”. Sets the position to fixed, making it stick to the top of the viewport after scrolling to a certain point.
  • Set top to 20px positions the sticky div 20 pixels from the top and set width to 100vw makes the div full width of the viewport.
  • Set the margin-bottom to 20px adds spacing below the fixed div.

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

HTML
<!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:



Similar Reads

How to make div that contains images to have fixed size using Bootstrap ?
In this article, we will see how we can make a div that will contain images having a fixed size. This can be very helpful when designing a webpage for displaying a certain type of product. We will use bootstrap to implement the solution for the above problem. Bootstrap CDN link: https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.c
1 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
Hide scroll bar, but while still being able to scroll using CSS
Hiding the scrollbar with CSS enhances webpage aesthetics by removing visible scrollbars while maintaining scroll functionality. This can be achieved using browser-specific CSS properties for Chrome, Safari, Firefox, and Internet Explorer, ensuring a cleaner, more streamlined user interface without sacrificing usability. There are many other option
3 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
How to Create a div that Contains Multiple Fixed-Size Images?
A &lt;div&gt; tag can be defined as a divider of two blocks of HTML code. It is used as a container for HTML elements which can then be styled with CSS or JavaScript. Class or id attribute can be used to easily style the element in the &lt;div&gt; tag. and we can style the class by using the ".class_name" in the CSS file same as "#id_name" for an i
2 min read
Animate a div on mouse scroll using AOS
AOS is a small library to animate elements on your page as you scroll. AOS allows you to animate elements as you scroll down, and up. If you scroll back to top, elements will animate to it's previous state and are ready to animate again if you scroll down. Installation: Installing AOS is very simple: Add the below tag in the head tag of the webpage
1 min read
How to Scroll to Bottom of Div in JavaScript ?
Scrolling to the bottom of a div is a common task in web development, especially when dealing with chat applications, message boards, or any content where new information is frequently added at the bottom. There are several approaches to scrolling to the bottom of the div in JavaScript which are as follows: Table of Content Using scrollTop Property
4 min read
How to Scroll to an Element Inside a Div using JavaScript?
To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript: Table of Content Using scrollIntoView( ) method Using scroll( ) method Using scrollT
3 min read
JavaScript Call a function after a fixed time
In order to run a function multiple times after a fixed amount of time, we are using few functions.  JavaScript setInterval() MethodThis method calls a function at specified intervals(in ms). This method will call continuously the function until clearInterval() is run, or the window is closed. Syntax:  setInterval(fun, msec, p1, p2, ...)Parameters:
3 min read
How to set smooth scroll after clicking the link using JavaScript ?
Smooth scroll provides a seamless user experience as it provides an effective way to enhance website navigation. By implementing smooth scroll functionality, one can ensure that users transition fluidly between different sections of your webpage when they click on internal links. There are several methods to implement smooth scroll after clicking t
4 min read
How to show Toast at Bottom-left after Scroll Down in Bootstrap 5 ?
Toasts are quick, gentle pop-up messages that keep users informed without interrupting. To achieve this functionality bootstrap does not provide any built-in class. But we can achieve it by combining utility classes like position-fixed, bottom-0, and start-0, and you can position the toast at the bottom-left corner of the viewport. Syntaxif (scroll
3 min read
How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
How to center a div within another div?
The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed and that division/section works like a container whose CSS styling can be done as a single unit or javascript can be used to perform various tasks on that container. Syntax: &lt;div&gt; &lt;h3&gt;'This is a div of the web page.&gt;/h
4 min read
How to center a div on the edge of another div in SASS ?
In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. Centering a div on the edge of another div is not a difficult task. We can achieve it b
4 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to clear all div's content inside a parent div ?
In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the
2 min read
How to Overlay/Hover Div in Flexbox Container Div?
In CSS, overlay/hover effects can be achieved by using Flexbox containers to center content and applying styles to reveal overlays on hover. By positioning elements absolutely within a Flexbox container, overlays can cover the entire content area. These are the following approaches: Table of Content Using ::before Pseudo-elementUsing an Overlay Div
3 min read
How to make your page scroll to the top when route changes?
Clicking the "About Us" button in a React application doesn't automatically scroll to the top of the page load, as react-router-dom only changes the route without resetting the scroll position; a separate functional component is needed to ensure the new page loads from the top when the route changes. Prerequisite:ReactStyled-ComponentsuseEffect Rea
3 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 hide div element after few seconds in jQuery ?
Given a div element, the task is to hide the div element after a few seconds using jQuery. Approach:Select the div element.Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.Example 1: In this example, the setTimeOut() method is used to provide a delay to the fadeOut() method. C/C++ Code &lt;!DOCTYPE HTML&gt;
2 min read
How to Make Entire Custom Checkbox/Div Clickable ?
In this article, we are going to learn how to make the entire custom checkbox/Div clickable. A custom checkbox is achieved by hiding the default checkbox input and styling a label element to resemble a checkbox using CSS. The label is associated with the checkbox using the for attribute, and the appearance changes when the checkbox is checked using
2 min read
How to make div with left aligned text and right aligned icon using CSS ?
In this article, we will learn to create an HTML div that has left-aligned text and a right-aligned icon. Sometimes, we need to align the text to the left side &amp; if there is an image or icon used then align it to the right side. For this, we can use the display property by setting the value as a flex property that is used to display an element
3 min read
How to make div not larger than its contents using CSS?
There are three ways to achieve this problem: By default case Using inline-block property Using fit-content property in width and height Default Case: In HTML div is by default fit to content inside it. The example goes like this: Example 1: &lt;!DOCTYPE html&gt; &lt;html lang = &quot;en&quot; dir = &quot;ltr&quot;&gt; &lt;head&gt; &lt;meta charset
3 min read
How to make a Bootstrap 4 accordion collapse when clicking the whole header div ?
In Bootstrap 4, Accordion collapse when anchor or button tag is clicked but lets see how to make a Bootstrap 4 accordion collapse when clicking the whole header with the help of following simple approach. First you have to remove anchor tag and button tag within div of class="card-header" except card title. Now add class collapsed and card-link to
2 min read
How to make a div span two rows in a grid using CSS ?
Suppose we have 5 elements in a row and the task to put a bigger element in the middle of a row. How to make a DIV span the 2 rows of the grid with the help of CSS. Approach 1: First get the height of the outer DIV of ID('outer'). We know the height of the outer element now design can be achieved using CSS Flexbox with flex-direction: column and fl
2 min read
How to make a div vertically scrollable without content using CSS ?
In this article, we will see how we can create a vertical scrollable section using CSS. HTML code is used to create the basic structure of the sections and CSS code is used to set the style, HTML Code: Create an HTML div element with the class container.Created three more nested div with class boxWrite some content inside each div with the class bo
2 min read
How to make an empty div take space ?
In this article, we will learn how to make an empty div take space using CSS. Approach: The solution is to give some height (or min-height) and width (or min-width) to your HTML div. When you set a particular height and width for a div, it does not matter if it is empty or overflowed, it will only take up the space as given in the code. Example: In
1 min read
How to make div elements display inline using CSS ?
In this article, we will learn the different approaches to displaying div elements inline. This will allow them to take available space horizontally on the screen in a line. First, we will create a basic HTML code for the div elements and apply different CSS styling to make it display inline. CSS properties: In this article, we will use below CSS p
3 min read