How to Add a Table of Contents in Blogger Website (Automatic)
Adding a Table of Contents (TOC) to your Blogger website helps readers navigate through your content easily. In this guide, we'll show you how to automatically generate a TOC for your Blogger posts.
Why Add a Table of Contents?
A TOC improves the user experience by providing quick access to different sections of your blog post. It also enhances SEO by helping search engines understand the structure of your content.
Steps to Add a Table of Contents
Follow these steps to add a TOC to your Blogger website:
Step 1: Add HTML and CSS
First, add the following HTML and CSS to your Blogger post:
CSS Codes
<style>
#toc {
position: sticky;
top: 20px;
background-color: #f5f5fa;
padding: 20px;
border-radius: 15px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
max-height: auto;
overflow-y: auto;
}
#toc a {
color: #c62641;
text-decoration: none;
display: block;
padding: 8px 10px;
border-radius: 5px;
transition: background-color 0.3s, color 0.3s;
margin-left: ${(parseInt(heading.tagName[1]) - 1) * 20}px;
}
#toc a:hover {
background-color: #c62641;
color: #ffffff;
}
#toc a.active {
font-weight: bold;
}
#toc a span {
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
color: #c62641;
}
@media (max-width: 768px) {
#toc {
width: 100%;
}
}
</style>
HTML Codes
<article class="post-content">
<div id="toc-placeholder"></div><br>
Paste Blog Post Here
</article>
Step 2: Add JavaScript
Next, add the following JavaScript to dynamically generate the TOC:
<script>
document.addEventListener('DOMContentLoaded', function () {
const tocPlaceholder = document.getElementById('toc-placeholder');
if (!tocPlaceholder) return;
const tocContainer = document.createElement('div');
tocContainer.setAttribute('id', 'toc');
const tocTitle = document.createElement('h3');
tocTitle.innerText = 'Table of Contents';
tocContainer.appendChild(tocTitle);
const content = document.querySelector('.post-content');
if (!content) return;
const headings = content.querySelectorAll('h1, h2, h3, h4, h5, h6');
if (headings.length === 0) return;
headings.forEach((heading) => {
const id = heading.innerText.replace(/\s+/g, '-').toLowerCase();
heading.setAttribute('id', id);
const anchor = document.createElement('a');
anchor.setAttribute('href', `#${id}`);
anchor.innerText = heading.innerText;
anchor.addEventListener('click', function (event) {
event.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
const bullet = document.createElement('span');
anchor.insertBefore(bullet, anchor.firstChild);
tocContainer.appendChild(anchor);
});
tocPlaceholder.appendChild(tocContainer);
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
const id = entry.target.getAttribute('id');
const tocItem = tocContainer.querySelector(`[href="#${id}"]`);
if (entry.isIntersecting) {
tocItem.classList.add('active');
} else {
tocItem.classList.remove('active');
}
});
}, { rootMargin: '-50% 0px -50% 0px' });
headings.forEach(heading => {
observer.observe(heading);
});
// Additional features:
// 1. Highlight the active heading section in the content area
window.addEventListener('scroll', function () {
headings.forEach((heading) => {
const rect = heading.getBoundingClientRect();
if (rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)) {
heading.classList.add('active-heading');
} else {
heading.classList.remove('active-heading');
}
});
});
// 2. Show a tooltip when hovering over the table of contents item
const tocItems = tocContainer.querySelectorAll('a');
tocItems.forEach((item) => {
item.addEventListener('mouseover', function () {
const tooltip = document.createElement('span');
tooltip.textContent = this.innerText;
tooltip.style.position = 'absolute';
tooltip.style.top = '-30px';
tooltip.style.backgroundColor = '#c62641';
tooltip.style.color = '#ffffff';
tooltip.style.padding = '5px';
tooltip.style.borderRadius = '5px';
tooltip.style.zIndex = '999';
this.appendChild(tooltip);
});
item.addEventListener('mouseout', function () {
this.querySelector('span').remove();
});
});
// 3. Add smooth scrolling for internal links in the content area
const internalLinks = document.querySelectorAll('a[href^="#"]');
internalLinks.forEach((link) => {
link.addEventListener('click', function (event) {
event.preventDefault();
const targetId = this.getAttribute('href').slice(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
});
});
});
</script>
Conclusion
By following these steps, you can easily add an automatic Table of Contents to your Blogger website. This enhances the user experience and improves SEO. Customize the CSS and JavaScript as needed to match your blog's design and functionality.

Post a Comment