CSS Tricks: Make It Sticky

Shares

Demo: Only view able in Firefox and Chrome browsers.

Scroll to see the sticky element sticking


Sticky Sticky Sticky

Sticky elements are elements on a page that don’t scroll out of view – they “stick to the visible area (viewport or scrolling box). You can create them with CSS using position: sticky;.

They act like relatively positioned elements before any scrolling and later like fixed elements once a scrolling threshold is reached. For now, only Firefox and Chrome support it.

HTML:

<h4>Scroll to see the sticky element <em>sticking</em></h4>
<br />
<div id="wrapper">
  <div id="sticky">
    Sticky Sticky Sticky
  </div>
</div>
<br />

CSS:

#sticky {
  position: sticky;
  background: #F762BC;
  width: 340px;
  height: 100px;
  top: 10px;
  left: 5px;
  display: flex;
  justify-content:center;
  align-items:center;
  box-shadow: 0 0 6px #000;
  text-shadow: 0 0 4px #fff
}
#wrapper {
  width: 75%;
  margin: auto;
  height: 800px;
  background-color: #c1c1c1;
}
}
h4{
  text-align: center;
}

Subscribe to the JqueryPlugins.net mailing list:
* indicates required

Reply

Pin It on Pinterest