Pen Settings

HTML

CSS

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

JavaScript

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

Packages

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.

Behavior

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.

HTML

              
                <html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>

  <main>
    <aside>
      <div  id="post__sidebar-left">
        <h4>Paragraphs</h4>
      </div>
    </aside>

    <div class="post__content">
      <h2 id="hello">Hello</h2>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu dui sit amet diam mollis facilisis sed sed lectus. Mauris vel risus maximus, molestie eros eu, lacinia massa. Ut quis ornare risus. Praesent vestibulum commodo dolor eu lacinia.
      <h3 id="subtitle-1">Subtitle 1</h3>
      Sed faucibus, purus quis laoreet commodo, ante tellus auctor felis, faucibus tincidunt sapien nisi eu leo.
      <h3 id="subtitle-2">Subtitle 2</h3>
      Sed faucibus, purus quis laoreet commodo, ante tellus auctor felis, faucibus tincidunt sapien nisi eu leo.
      <h2 id="another-title">Another title</h2>
      Sed vulputate turpis massa, eget hendrerit nunc ullamcorper sed. Phasellus porttitor aliquet faucibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque gravida diam id erat interdum, eu bibendum massa interdum. Maecenas cursus nulla quis enim mollis accumsan. Aenean id blandit libero.
    </div>
  </main>
</html>
              
            
!

CSS

              
                html {
  background-color: rgb(25, 25, 25);
  color: rgb(220, 220, 220);
}

a {
  color: paleturquoise;
}

main {
  display: flex;
  max-width: 400px;
}

#post__sidebar-left {
  padding: 10px;
  position: sticky;
  top: 0;
}

#post__sidebar-left a.current {
  color: gold;
}
              
            
!

JS

              
                let headers;
let sidebarLinks;

$(document).ready(function () {
    headers = $(".post__content h2, .post__content h3").toArray();
    const sidebar = $("#post__sidebar-left");

    for (let i = 0; i < headers.length; i++) {
        const currentHeader = headers[i];
        const headerText = $(currentHeader).text();
        const headerId = $(currentHeader).attr("id");
        const headerType = $(currentHeader).prop("nodeName").toLowerCase();

        // <h2> <a href="header-linke"> Header Name </a> </h2>
        const newLine = "<" + headerType + "> <a href=\"#" + headerId + "\">" + headerText + "</a> </" + headerType + ">";
        sidebar.append(newLine);
    }

    sidebarLinks = $("#post__sidebar-left a").toArray();
    
    $(window).scroll(function () {
        let bottomScroll = $(window).scrollTop() + ($(window).height() / 2);
        let found = false;

        for (let i = headers.length - 1; i >= 0; i--) {
            const currentHeader = headers[i];
            const currentSidebarLink = sidebarLinks[i];
            $(currentSidebarLink).removeClass("current");

            const headerVPPosition = $(currentHeader).offset().top;

            if (!found && bottomScroll > headerVPPosition) {
                $(currentSidebarLink).addClass("current");
                found = true;
            }
        }
    });
});
              
            
!
999px

Console