Introduction
Closed TOC:
Step 1: Adding CSS to the template Code
.toc-container {
position: relative;
}
.toc-container .mbtTOC {
border: none;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
background-color: #ffffff;
color: #333;
line-height: 1.4em;
margin: 30px auto;
padding: 20px 30px;
font-family: 'Roboto', sans-serif;
width: 350px; /* Increased width for desktop */
position: fixed;
top: 10px;
right: 10px;
z-index: 1000;
border-radius: 8px;
transition: all 0.3s ease;
}
.toc-container .mbtTOC button {
background: #077ea7;
font-family: 'Roboto', sans-serif;
font-size: 18px;
position: relative;
outline: none;
cursor: pointer;
border: none;
color: #ffffff;
padding: 10px 15px;
display: block;
width: 100%;
text-align: left;
border-radius: 8px;
transition: background 0.3s ease;
}
.toc-container .mbtTOC button:hover {
background: #4CAF50;
}
.toc-container .mbtTOC button .toggle-text {
font-size: 16px;
color: #ffffff;
float: right;
}
.toc-container .mbtTOC ul {
list-style: none;
margin: 0;
padding: 0;
display: none; /* Initially hidden */
}
.toc-container .mbtTOC ul li {
padding: 10px 0;
font-size: 16px;
}
.toc-container .mbtTOC a {
color: #0871b2;
text-decoration: none;
display: block;
padding: 5px 0;
transition: color 0.3s ease;
}
.toc-container .mbtTOC a:hover {
color: #0826b2;
}
.toc-container .toc-h1 {
font-size: 18px;
font-weight: bold;
margin-left: 0;
}
.toc-container .toc-h2 {
font-size: 16px;
margin-left: 20px;
}
.toc-container .toc-h3 {
font-size: 14px;
margin-left: 40px;
}
@media (min-width: 360px) {
.toc-container .mbtTOC {
width: 90%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
@media (min-width: 468px) {
.toc-container .mbtTOC {
width: 70%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
/* Responsive */
@media (min-width: 768px) {
.toc-container .mbtTOC {
width: 70%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
@media (min-width: 980px) {
.toc-container .mbtTOC {
width: 70%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
@media (min-width: 1200px) {
.toc-container .mbtTOC {
width: 70%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
@media (min-width: 1440px) {
.toc-container .mbtTOC {
width: 70%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
@media (min-width: 1680px) {
.toc-container .mbtTOC {
width: 70%;
position: static;
margin: 10px auto;
box-shadow: 0 2px 5px rgb(0 0 0 / 30%);
}
}
Step 2: Adding JavaScript for the functioning of TOC
document.addEventListener("DOMContentLoaded", function() { const tocButton = document.getElementById("toc-toggle"); const tocList = document.getElementById("toc-list"); tocButton.addEventListener("click", function() { tocList.style.display = tocList.style.display === "none" ? "block" : "none"; }); // Generate TOC const headings = document.querySelectorAll("h1, h2, h3"); let tocHTML = ""; let h1Count = 0, h2Count = 0, h3Count = 0; let stopIndexing = false; headings.forEach((heading, index) => { if (stopIndexing) return; const level = heading.tagName.toLowerCase(); let number = ""; if (level === "h1") { h1Count++; h2Count = 0; h3Count = 0; number = `${h1Count}.`; } else if (level === "h2") { h2Count++; h3Count = 0; number = `${h1Count}.${h2Count}.`; } else if (level === "h3") { h3Count++; number = `${h1Count}.${h2Count}.${h3Count}.`; } tocHTML += `<li class="toc-${level}"><a href="#heading-${index}">${number} ${heading.innerText}</a></li>`;
heading.id = `heading-${index}`; if (heading.innerText.trim().toLowerCase() === "conclusion") { stopIndexing = true; } }); tocList.innerHTML = tocHTML; // Add smooth scrolling tocList.querySelectorAll('a').forEach(link => { link.addEventListener('click', function(event) { event.preventDefault(); document.querySelector(this.getAttribute('href')).scrollIntoView({ behavior: 'smooth' }); }); }); });
Final step: To add HTML code
<div class="toc-container">
<div class="mbtTOC">
<button id="toc-toggle">Table of Contents <span class="toggle-text">Show/Hide</span></button>
<ul id="toc-list">
</ul>
</div>
</div>
<p></p>
<br>






.gif)
