Languages
[Edit]
EN

CSS - sticky position for header and footer element

8 points
Created by:
Saim-Mccullough
568

In this short article, we would like to show how to use both sticky positionings for the header and footer of elements with pure CSS.

Note: to see better description how sticky works and how to use top positioning read this article.

Example solution:

// 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;
    }
    
    div.footer {
        position: -webkit-sticky;  /* <----- required (for WebKit) */
        position: sticky;  /* <------------- required              */
        bottom: 10px;  /* <----------------- required              */
		border: 1px solid red;
      	background: silver;
    }

  </style>
</head>
<body>
  <p>Some text here...</p>
  <div>
    <div class="header">Header 1</div>
    <div class="content">
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
    </div>
    <div class="footer">Footer 1</div>
  </div>
  <p>Some text here...</p>
  <p>Some text here...</p>
  <p>Some text here...</p>
</body>
</html>

 

Many items example

In this section, we want to show how to use sticky with multiple items. It is good to add additional margins to the previous example to not let footers cover headers.

// ONLINE-RUNNER:browser;

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

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

  </style>
</head>
<body>

  <p>Some text here...</p>
  <p>Some text here...</p>
  <p>Some text here...</p>

  <div>
    <div class="header">Header 1</div>
    <div class="content">
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
    </div>
    <div class="footer">Footer 1</div>
  </div>

  <p>Some text here...</p>
  <p>Some text here...</p>
  <p>Some text here...</p>

  <div>
    <div class="header">Header 2</div>
    <div class="content">
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
    </div>
    <div class="footer">Footer 2</div>
  </div>

  <p>Some text here...</p>
  <p>Some text here...</p>
  <p>Some text here...</p>

  <div>
    <div class="header">Header 3</div>
    <div class="content">
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
      Some text here...<br />
    </div>
    <div class="footer">Footer 3</div>
  </div>

  <p>Some text here...</p>
  <p>Some text here...</p>

  <p>Some text here...</p>
</body>
</html>

 

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.

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