Languages
[Edit]
EN

CSS - top sticky position for div element

8 points
Created by:
James-Z
337

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>
<html>
<head>
  <style>
    
    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;
    }

  </style>
</head>
<body>
  <p>Some text here...</p>
  <div>
    <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 />
    </div>
  </div>
  <p>Some text here...</p>
  <p>Some text here...</p>
  <p>Some text here...</p>
</body>
</html>

 

CSS

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.

â€ïžđŸ’» 🙂

Join