Languages
[Edit]
EN

CSS - sticky position for header and footer element

5 points
Created by:
Marcin
2535

In this short article we would like to show how to use both sticky positioning for header and footer of element 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 ection we want to show how to use sticky with multiple items. It is good to add additional margins to previous example to do not let footers to 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>

 

Hey ūüĎč
Would you like to know what we do?
  • Dirask is online IT community for professionals and hobbyist 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.