CSS - top sticky position for div element

8 points
Created by:

In this short article we would like to show how to use sticky top positioning with pure CSS.

In practice we can use it to keep the item header visible during scrolling.

To enable sticky positioning position: sticky; and one of the: top, left, right or bottom property is needed.

In the article we will use top one.

How sticky works?

It glues the element depending of top, left, right or bottom property - it do not let the element to disappear during scrolling using fixed position when element border touches bound set by the property (top, left, etc.).

Simple example:

// ONLINE-RUNNER:browser;

<!doctype html>
    body {
    	max-height: 300px;

    div.header {
        position: -webkit-sticky;  /* <----- required (for WebKit) */
        position: sticky;  /* <------------- required              */
        top: 10px;  /* <-------------------- required              */
        border: 1px solid red;
      	background: silver;
    div.content {
    	border: 1px solid orange;
      	height: 1000px;

  <p>Some text here...</p>
    <div class="header">Item 1</div>
    <div class="content">
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
  <p>Some text here...</p>
  <p>Some text here...</p>
  <p>Some text here...</p>


Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.