Javatpoint Logo
Javatpoint Logo

CSS Sticky

The CSS position property sets the position for an HTML element. It is also used to place an item behind another element and is useful for the scripted animation effect. The "position: sticky;" is used to position the element based on the user's scroll position.

This CSS position property allows the elements to stick when the scroll reaches a certain point. Depending on the scroll position, a sticky element toggles between fixed and relative. The element will be positioned relative until the specified position of offset is met in the viewport. Then, similar to position: fixed, the element sticks in one place.

Illustrations of the CSS Position: Sticky

Let us comprehend the CSS property properly with the help of illustrations.

Illustration 1:

We will create the sticky element in this illustration with the help of CSS.

Code:

Output:

We can witness in the output that the HTML element in sky blue is not moving even after scrolling as it is the sticky element, so it will stay where it is.

CSS sticky

Illustration 2:

We will construct the sticky navbar with the help of the CSS sticky position in this illustration.

Code:

Output:

We can witness in the output that the navbar is not moving when scrolling as it is the sticky element so that it will remain in the same place.

CSS sticky

Illustration 3:

We will construct 1 sticky main heading and 2 sticky paragraph headings with the help of the CSS sticky position in this illustration.

Code:

Output:

We can witness in the output that the main heading and 2 paragraph headings are not moving when we scroll because they are sticky elements.

CSS sticky

Illustration 4:

We will construct a sidebar and set it to the sticky position with the help of the CSS sticky position in this illustration.

Code:

Output:

We can witness that the sidebar remains in the same place even after scrolling because it is set to the sticky position.

CSS sticky

Browser Compatibility

The browsers that support the CSS sticky are given below:

  • Internet Explorer
  • Firefox
  • Safari
  • Google Chrome
  • Opera

Conclusion

We have comprehended the CSS sticky in this article. The CSS sticky is the value assigned to the CSS position property to make the HTML element stick to one place when it reaches its threshold.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA