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

              
                
<div>
  <div id="toc-container"></div>

</div>


<div style="font-size:40px;">
  <h2>Lorem ipsum dolor h2</h2>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
  <h2>Lorem ipsum dolor h2</h2>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

  <h3>Lorem ipsum dolor h3</h3>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function() {
    // Select all h2 and h3 elements
    const headers = document.querySelectorAll("h2, h3");

    // Find the specified container for the table of contents
    const tocContainer = document.getElementById("toc-container");
    if (!tocContainer) {
        console.error("Table of contents container not found.");
        return;
    }

    // Create a container for the table of contents
    const toc = document.createElement("div");
    toc.id = "table-of-contents";
    toc.innerHTML = "<h2>Table of Contents</h2>";

    // Create an unordered list for the links
    const list = document.createElement("ul");

    // Iterate over the headers and create list items with links
    headers.forEach((header, index) => {
        // Assign a unique ID to each header
        header.id = "heading-" + index;

        // Create a list item and a link for the table of contents
        const listItem = document.createElement("li");
        const link = document.createElement("a");
        link.href = "#" + header.id;
        link.innerText = header.textContent;
        listItem.appendChild(link);

        // Append the list item to the unordered list
        list.appendChild(listItem);
    });

    // Append the unordered list to the table of contents container
    toc.appendChild(list);
    tocContainer.appendChild(toc);

    // Optional: Add smooth scrolling
    list.addEventListener("click", function(event) {
        if (event.target.tagName === "A") {
            event.preventDefault();
            const targetId = event.target.getAttribute("href").slice(1);
            document.getElementById(targetId).scrollIntoView({
                behavior: "smooth"
            });
        }
    });
});

              
            
!
999px

Console