Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <p>Table of Contents :</p>
<ul id="toc"></ul>



<h1>Simple JavaScript TOC</h1>
<h2>How to use?</h2>
<p>Simply include the JS & CSS in your page and add a UL element with “toc” as ID. That’s it.</p>
<h2>Features</h2>
<p>If your headings do have IDs, they will be detected and the TOC will contain internal links pointing to the headings in that page.</p>
<p>If they do not, they will be added, thanks to <a href="https://codepen.io/prx__/pen/OJVemWE">prx's pen</a>.</p>
<h2>Part 3</h2>
<p>Dolor sit amet…</p>
<h2>Part 4</h2>
<h3>Lorem Ipsum</h3>
<p>Lorem ipsum…</p>
<h3>P4 - Chapter 1</h3>
<h4>P4-C1 - section 1</h4>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>
<h4>P4-C1 - section 2</h4>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>
<h3>P4 - Chapter 2</h3>
<h4>P4-C2 - section 1</h4>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>
<h2>Part 5</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h6>h6 bis</h6>
              
            
!

CSS

              
                h1, h2, h3, h4 {
  text-align: center;
}

#toc {
  color: red;
}


/* Add counters on headings */
/* None is added on H1 elements, because it is supposed to be unique on a page. But one could add it if needed. If so, only addapt the CSS. */
body {
  counter-reset: countH2;
}
h2 {
	counter-reset: countH3;
	counter-increment: countH2;
}
h3 {
	counter-reset: countH4;
	counter-increment: countH3;
}
h4 {
	counter-reset: countH5;
	counter-increment: countH4;
}
h5 {
	counter-reset: countH6;
	counter-increment: countH5;
}
h6 {
	counter-increment: countH6;
}

h2::before {
  content: counter(countH2) " - ";
}
h3::before {
  content: counter(countH2) "." counter(countH3) " - ";
}
h4::before {
  content: counter(countH2) "." counter(countH3) "." counter(countH4) " - ";
}
h5::before {
  content: counter(countH2) "." counter(countH3) "." counter(countH4) "." counter(countH5) " - ";
}
h6::before {
  content: counter(countH2) "." counter(countH3) "." counter(countH4) "." counter(countH5) "." counter(countH6) " - ";
}

/* Adding counters on the TOC LIs */
#toc,
#toc ul {
  list-style-type: none;
}
#toc {
	counter-reset: nav0;
}
#toc > li {
	counter-increment: nav0;
	counter-reset: nav1;
}
#toc > li li {
	counter-increment: nav1;
	counter-reset: nav2;
}
#toc > li li li {
  counter-increment: nav2;
  counter-reset: nav3;
}
#toc > li li li li {
	counter-increment: nav3;
  counter-reset: nav4;
}
#toc > li li li li li {
	counter-increment: nav4;
  counter-reset: nav5;
}
#toc > li li li li li li {
	counter-increment: nav5;
  counter-reset: nav6;
}


#toc > li::before {
	content: counter(nav0)" - ";
}
#toc > li li::before {
	content: counter(nav0)"."counter(nav1) " - ";
}
#toc > li li li::before {
	content: counter(nav0)"."counter(nav1)"."counter(nav2)" - ";
}
#toc > li li li li::before {
	content: counter(nav0)"."counter(nav1)"."counter(nav2)"."counter(nav3)" - ";
}
#toc > li li li li li::before {
	content: counter(nav0)"."counter(nav1)"."counter(nav2)"."counter(nav3)"."counter(nav4)" - ";
}
#toc > li li li li li li::before {
	content: counter(nav0)"."counter(nav1)"."counter(nav2)"."counter(nav3)"."counter(nav4)"."counter(nav5)" - ";
}
#toc > li li li li li li li::before {
	content: counter(nav0)"."counter(nav1)"."counter(nav2)"."counter(nav3)"."counter(nav4)"."counter(nav5)"."counter(nav6)" - ";
}

              
            
!

JS

              
                var TOC = document.getElementById('toc'),
headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6'),
parentLevel = 1,
cursorNode = TOC;

for (var i = 0, len = headings.length ; i < len ; ++i) {
	var currentHeading = headings[i];
	var newLevel = parseInt(currentHeading.tagName.substr(1,1));
	var diff = newLevel - parentLevel;
	if (diff > 0) {
		var containerLiNode = cursorNode.lastChild;
		var ulNode = document.createElement('UL')
		containerLiNode.appendChild(ulNode);
		cursorNode = ulNode;
		parentLevel = newLevel;		
	}
	if (diff < 0) {
		while (0 !== diff++) {
			cursorNode = cursorNode.parentNode.parentNode;
		}
		parentLevel = newLevel;
	}

	var liNode = document.createElement('LI');
	if (!currentHeading.hasAttribute('id')) {
    currentHeading.id = 'h' + i;
  }
  var link = document.createElement('A');
	link.setAttribute('href', '#' + currentHeading.getAttribute('id'));
	link.appendChild(document.createTextNode(currentHeading.textContent))
	liNode.appendChild(link);
  cursorNode.appendChild(liNode);
}
              
            
!
999px

Console