Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <div class="lorem">
  <h1>Sticky nav with IntersectionObserver</h1>
  <p style="color: darkred">⚠️ This demo doesn't work in CodePen's iframe <a href="">because it is on a different domain</a>. You can fork it and open the <code>debug</code> view.</p>
<div class="scroller">
      <li><a href="#block-1">One</a></li>
      <li><a href="#block-2">Two</a></li>
      <li><a href="#block-3">Three</a></li>
      <li><a href="#block-4">Four</a></li>
      <li><a href="#block-5">Five</a></li>
  <div class="block block-1" id="block-1">One</div>
  <div class="block block-2" id="block-2">Two</div>
  <div class="block block-3" id="block-3">Three</div>
  <div class="block block-4" id="block-4">Four</div>
  <div class="block block-5" id="block-5">Five</div>
<div class="lorem">Lorem ipsum…</div>
<div class="lorem">Lorem ipsum…</div>
<div class="lorem">Lorem ipsum…</div>
<div class="lorem">Lorem ipsum…</div>


                html {
  scroll-behavior: smooth;
body {
  margin: 0;
  font-family: sans-serif;
.lorem {
  height: 300px;
  padding: 1rem;
  background-color: #ddd;
nav {
  position: sticky;
  top: 0;
  padding-block: 1rem;
  background-color: #fff;
  left: 0;
  width: 100%;
ul {
  list-style: none;
  padding-left: 0;
  margin: 0;
  display: flex;
  column-gap: 1rem;
  justify-content: center;
nav a {
  text-decoration: none;
  color: inherit;
  transition: color 0.2s ease-in;
.is-active {
  color: blue;
  font-weight: bold;
  text-decoration: underline;
.block {
  height: 60vh;
  scroll-margin-top: 50px;
  padding: 1rem;
  color: white;
.block-1 {
  background-color: rebeccapurple;
.block-2 {
  background-color: darkgreen;
.block-3 {
  background-color: silver;
  height: 3rem;
.block-4 {
  background-color: grey;
.block-5 {
  background-color: salmon;



                // Restrict intersection zone to a 1px line below the nav
const topMargin = document.querySelector(".scroller nav").offsetHeight + 1;
const bottomMargin = window.innerHeight - topMargin;
const observerOptions = {
  rootMargin: `-${topMargin}px 0px -${bottomMargin}px 0px`

const onIntersect = (entries, observer) => {
  entries.forEach((entry) => {
    const entryNav = document.querySelector(`[href="#${}"]`)
    if (entry.isIntersecting) {
      // Only one entry should be intersecting
      console.log(`Now intersecting: ${}`);
    } else {
      console.log(`Not intersecting anymore: ${}`);

const observer = new IntersectionObserver(onIntersect, observerOptions);

document.querySelectorAll(".block").forEach((section) => {