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>


Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.


Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂