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="page">
    <a href="#s1">Block 1</a>
    <a href="#s2">Block 2</a>
    <a href="#s3">Block 3</a>
    <a href="#s4">Block 4</a>
    <a href="#footer">Footer</a>
    <section id="s1" style="height:70px">
       Block 1
    <section id="s2" style="height:250px">
       Block 2
    <section id="s3" style="height:85px">
       Block 3
    <section id="s4" style="height:500px">
       Block 4
    <section id="footer" style="height:1500px">


                html {
  scroll-behavior: smooth;
main {
  padding-top: 50px;
section {
  scroll-margin: 50px;
  padding: 20px;
  &:nth-child(2n) {
    background-color: orange;
nav {
  z-index: 99;
  position: fixed;
  background: lightgreen;
  width: 100%;
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  a {
    &.active {
      background: yellow;


                // callback function for the intersection observer.
const changeNav = (entries, observer) => {
  let updated = false; // whether the menu was already updated during this intersection.
	entries.forEach((entry) => {
    // if the menu was aleady updated by a previous entry, we don't need to check the other entries.
    if (updated) {
    let activeBlock; // the block that should be highlighted in the menu.
    if(!entry.isIntersecting) {
      // This element has just left the viewport. This means at least one of 2 possibilities:
      // 1. the next element has its top aligned with the top of the viewport.
      //    In this case the next element should be activated.
      // 2. the previous element is (at least partly) visible.
      //    In this case we need to keep looking at previous elements until
      //    we find the highest one that is still visible.
      if (isInViewport( {
        // possibility 1.
        activeBlock =;
      } else if (isInViewport( {
        // possibility 2.
        activeBlock =;
        while ( isInViewport(activeBlock.previousElementSibling) ) {
          activeBlock = activeBlock.previousElementSibling;
    } else {
      // This element just entered the viewport. 2 possibilities:
      // 1. The top of the element moved in from the bottom.
      //    In this case nothing needs to happen.
      // 2. The bottom of the element moved in from the top.
      //    In this case the current element should also become active.
      if (!isInViewport( {
        // possibility 1.
      } else if (!isInViewport( {
        // possibility 2.
        activeBlock =;
    // remove active class on all elements.
    document.querySelectorAll('nav >').forEach((aEl) => {
    // Couldn't deternmine an active block. So don't do anything.
    if (!activeBlock) {

    // get id of the intersecting section
    const id = activeBlock.getAttribute('id');
    // find matching link and add appropriate class
    // Wer're done. Set flag to make sure that we don't check other entries.
    updated = true;

function isInViewport(element) {
    if (!element) {
      return false;
    const rect = element.getBoundingClientRect();
    return (
        rect.bottom >= 0 && <= (window.innerHeight || document.documentElement.clientHeight)

// init the observer
const options = {
  rootMargin: "-51px", // 1 pixel more than nav height
  root: document,
  treshhold: [],

const observer = new IntersectionObserver(changeNav, options);

// target the elements to be observed.
const sections = document.querySelectorAll('section');
sections.forEach((sectionNavTrigger) => {