Slider

Adding Table of Content (TOC) in Blogger.

Learn to add Manual and Automatic Table of Contents in Blogger to increase your opportunity opportunity to rank top in Google Search.
Table of Contents in Blogger

Creating engaging and easily navigable content is important for any blogger. Long, informative articles can be a treasure trove of information, but without proper organization, they can overwhelm readers and hinder their experience. This is where the Table of Contents (TOC) comes into play, acting as a roadmap that guides your audience through your blog posts.

Adding a table of contents isn't a built-in feature for Blogger users, like on some other platforms. However, fear not! This comprehensive guide will walk you through both manual and, more importantly, automatic methods to seamlessly integrate a TOC into your Blogger blog, significantly enhancing user experience and boosting your search engine optimization (SEO).

What is a Table of Contents (TOC)?

A Table of Contents (TOC) is a list of the main sections or topics within a document, book, or webpage, usually found at the beginning. It helps readers quickly see what's covered and jump directly to specific parts. Think of it as an interactive outline or a roadmap for the content.

Simple Definition

A Table of Contents is an organized list of headings and subheadings that shows what's in a document and where to find it. Each item in the list is typically a clickable link that jumps you to that section.

Why Should You Add a Table of Contents in Blogger?

I know you guys are more interested in the "how" section of this article, but it is also important to understand "why" TOC should be part of your long blog post and how it can improve user experience and SEO.

Here are some key benefits of a Table of Contents (TOC):

  1. Improved User Experience (UX): A TOC allows readers to quickly scan the content and jump directly to the sections that interest them most. This saves time, reduces frustration, and keeps them engaged with your article. Think of it as providing instant access to the information they're looking for.
  2. Enhanced SEO: Search engines like Google love well-structured content. A table of contents helps them better understand the hierarchy and key topics covered in your post. This can lead to:
    • Improved Crawlability: Search engine bots can more easily navigate and index your content.
    • Rich Snippets (Jump Links): Google may display jump links (links to specific sections within your article) directly in the search results, increasing your click-through rate (CTR).
    • Keyword Optimization: The headings in your TOC often contain relevant keywords, further signaling to search engines the topic of your post.
  3. Reduced Bounce Rate: When users can quickly find the information they need, they are more likely to stay on your page and explore further, thus reducing your bounce rate – a positive signal for search engines.
  4. Increased Time on Page: By making your content more digestible and navigable, you encourage readers to spend more time on your page, another crucial metric for SEO.
Okay! Now that we are both aware of the benefits of using TOC, not implementing this in our blog post will be unfair for our long informative articles. Let's learn different approaches to implement this, and later you can decide which one suits best for your needs.

How To Add a Table of Contents in Blogger.

There are generally two approaches to adding a table of contents in Blogger:

  1. Manual Table of Contents: This method involves manually identifying your headings, adding unique IDs to them in the HTML editor, and then creating a list of links that point to these IDs. While straightforward, this can be time-consuming, especially for long articles, and requires manual updates if you change your headings.

  2. Automatic Table of Contents: This more efficient approach utilizes JavaScript code to automatically scan your post for headings (H2, H3, etc.) and generate a linked table of contents. This method is dynamic – if you change your headings, the TOC updates automatically.

Key Note:

While the manual TOC has its place for very short, static pages, for the vast majority of blog posts and content-rich websites, the automatic TOC is undeniably superior in terms of overall performance.


Method 1: Creating a Manual Table of Contents.

The manual TOC relies on a fundamental HTML concept called anchor links. These links allow you to jump to a specific part of the same web page. To make this work, you need two things:

  1. An ID for the target element: A unique identifier (id) attached to the heading or section you want to link to.
  2. A link pointing to that ID: An <a> tag with its href attribute set to followed by the target element's id.

Step-by-Step Guide: Manually Adding TOC in Blogger.

Step 1: Open Your Blog Post in HTML View.

Go to your Blogger dashboard and click on "Posts" in the left sidebar. Either create a "New Post" or open an existing post you want to add a TOC to. 

In the post editor, you'll see two views: "Compose view" (the visual editor) and "HTML view" (the code editor). Click on the "HTML view" tab/button. This is crucial, as you'll be editing the underlying code.

HTML View of Blogger

Step 2: Add Unique IDs to your headings.

Now, you need to identify the headings (e.g., H2, H3, H4) in your post that you want to include in your TOC. For each of these headings, you'll add a unique id attribute.

  • Locate your headings: Scroll through the HTML view and find your heading tags (e.g.,<h2>, <h3>, <h4>).
  • Add an id attribute: Inside the opening tag of each heading, add id="your-unique-id".
    • Each id must be unique within that entire blog post.
    • IDs should contain only letters, numbers, hyphens (-), and underscores (_). Avoid spaces or special characters.
    • Make them descriptive but concise (e.g., id="introduction", id="step-one", id="benefits-of-x").
Adding ID to H2

Step 3: Create a Table of Contents List.

With your headings now uniquely identified, proceed to build the Table of Contents list itself, typically placed at the top of your post. You can achieve this by typing out your desired heading titles as a bulleted or numbered list in the "Compose view." 

For each list item, select the text, click the "Link" icon in the editor, and in the URL field, type a hashtag (#) followed directly by the exact ID you assigned to that corresponding heading in Step 2 (e.g., #introduction for the "Introduction" section). Repeat this linking process for every item in your TOC until all sections are correctly connected. 
TOC link creation

Or you can use the below sample code:

Example: Paste this code where you want to add the TOC in the post with updated ID and Headings.
<div class="toc-container">
  <h4>Table of Contents</h4>
  <ul>
    <li><a href="#introduction">Introduction</a></li>
    <li><a href="#key-features">Key Features</a></li>
    <li><a href="#setting-up">Setting Up</a></li>
    <li><a href="#troubleshooting">Troubleshooting</a></li>
  </ul>
</div>

Note: The div with toc-container and h4 for "Table of Contents" are added for structure, but the core is the ul and li with anchor links.

Step 4: Style Your Table of Contents (Optional but Recommended)

To ensure your manually created Table of Contents looks appealing and seamlessly integrates with your blog's overall design, you can apply custom CSS. This step allows you to control its visual appearance, from fonts and colors to spacing and borders.

To begin, navigate to your Blogger dashboard. From there, locate and click "Theme" in the left sidebar, then click the "Customize" button.

Within the customization options, find and select "Advanced," which will then present you with the choice to "Add CSS." This is the dedicated area where you will paste your custom styling code. After inserting your CSS, click "Save" to apply the changes to your blog's theme.
Advance Setting to Add CSS

Example CSS (you can adjust colors, fonts, margins, etc.):
.toc-container {
  border: 1px solid #ddd;
  padding: 15px;
  margin: 20px 0;
  background-color: #f9f9f9;
  border-radius: 5px;
}

.toc-container h4 {
  font-size: 1.1em;
  color: #333;
  margin-top: 0;
  margin-bottom: 10px;
}

.toc-container ul {
  list-style-type: none; /* Removes default bullets/numbers */
  padding-left: 0;
  margin-bottom: 0;
}

.toc-container li {
  margin-bottom: 8px;
}

.toc-container a {
  text-decoration: none;
  color: #007bff; /* Example link color */
  transition: color 0.3s ease;
}

.toc-container a:hover {
  color: #0056b3; /* Example hover color */
  text-decoration: underline;
}

Step 5: Preview and Publish Your Post.

Click the "Preview" button in the Blogger post editor to see how your TOC looks and if the links work correctly. If everything looks good, click "Publish" (or "Update" for an existing post).

The manual method is a good way to understand how anchor links work and is suitable for occasional, shorter posts where you need precise control. However, for a scalable and efficient solution, an automatic TOC is generally preferred.

Method 2: Implementing an Automatic Table of Contents.

This method is recommended for most Blogger users as it automatically generates the TOC based on your post's headings, saving you significant time and effort in the long run. It involves adding a bit of HTML, CSS, and JavaScript to your Blogger theme.

Step-by-step Guide: Automatic Table of Contents.

Step 1: Prepare your blog post headings.

Before adding any code, ensure your blog posts are structured correctly using Blogger's heading options. The automatic script will rely on these.

When writing your posts in "Compose view," always use the built-in heading styles (e.g., "Heading," "Subheading," "Minor heading") for your main sections and sub-sections. These correspond to HTML tags like <h2>, <h3>, <h4>, etc.

Step 2: Add the HTML placeholder for the TOC.

This is where your Table of Contents will actually appear in your blog posts. You'll add a simple div element.
  1. Open or Create a Post: Go to your Blogger dashboard, then "Posts," and open the post where you want the TOC to appear.
  2. Switch to HTML View: In the post editor, click on the "HTML view" tab/button.
  3. Insert the Placeholder: Decide where you want your TOC to show up. The most common and effective place is right after your introductory paragraph, before the first main heading (<h2>).
Note: This check ensures the script only runs if you've placed the code in your post, preventing errors on pages without a TOC.

Paste the following HTML Code:
<div id="blog-toc-container"></div>

Step 3: Add the Custom CSS for Styling the TOC.

From the Blogger dashboard, click on Theme > Customize > Advanced and select the "Add CSS" option from the drop-down. Copy the following CSS code and paste it into the "Add CSS" box. You can modify the values (colors, padding, margins, etc.) to match your blog's design.

CSS Code:
/* Table of Contents Styling */
#blog-toc-container {
  background: #fdfdfd; /* Light background for the box */
  border: 1px solid #e0e0e0; /* Subtle border */
  padding: 20px;
  margin: 25px 0; /* Space above and below the TOC */
  border-radius: 8px; /* Slightly rounded corners */
  box-shadow: 0 2px 5px rgba(0,0,0,0.05); /* Soft shadow */
}

#blog-toc-container .toc-title {
  font-size: 1.25em; /* Larger title font */
  font-weight: 700; /* Bold title */
  color: #333;
  margin-bottom: 15px; /* Space below title */
  text-align: center; /* Center the title */
}

#blog-toc-container ul {
  list-style: none; /* Remove default list bullets/numbers */
  padding-left: 0;
  margin: 0;
}

#blog-toc-container li {
  margin-bottom: 8px; /* Space between list items */
  line-height: 1.5;
}

#blog-toc-container a {
  text-decoration: none; /* Remove underline from links */
  color: #007bff; /* Link color */
  font-weight: 500;
  transition: color 0.3s ease; /* Smooth hover effect */
}

#blog-toc-container a:hover {
  color: #0056b3; /* Link hover color */
  text-decoration: underline;
}

/* Indentation for subheadings */
#blog-toc-container ul ul {
  margin-left: 5px; /* Indent for H3 */
  padding-left: 5px;
}

#blog-toc-container ul ul ul {
  margin-left: 5px; /* Indent for H4 */
  padding-left: 5px;
}

Click "Save" at the bottom right of the theme customizer.

Step 4: Add the JavaScript for Generating the TOC.

This code finds headings and creates a TOC dynamically.
  1. From the Blogger dashboard, click on Theme in the left sidebar and select "Edit HTML" from the drop-down.
  2. Locate the </body> Tag: Carefully scroll down to the very end of your theme's HTML code and find the closing </body> tag. Press Ctrl+F to find the exact keyword.
  3. Paste the following JavaScript code just before the </body> tag and click on the Save icon. This ensures the entire page content is loaded before the script tries to find the headings. 
Code to add Automatic Table of Contents in Blogger

JavaScript Code:
<script>
//<![CDATA[
document.addEventListener('DOMContentLoaded', function() {
    const tocContainer = document.getElementById('blog-toc-container');

    if (tocContainer) { // Ensure the TOC container exists on the page
        const postBody = document.querySelector('.post-body, .blog-post-body'); // Adjust based on your theme's content selector
        if (!postBody) {
            console.warn("Could not find post body. TOC generation aborted.");
            return;
        }

        const headings = postBody.querySelectorAll('h2, h3, h4'); // Select heading levels to include

        if (headings.length > 0) {
            let tocHtml = '<div class="toc-title">Table of Contents</div><ul class="toc-main-list">';
            let currentH2 = null;
            let currentH3 = null;

            headings.forEach((heading, index) => {
                // Generate a unique ID for the heading if it doesn't have one
                const headingId = heading.id || 'section-' + (index + 1);
                heading.id = headingId; // Assign the ID back to the heading

                const headingText = heading.textContent.trim();
                const tagName = heading.tagName.toLowerCase(); // h2, h3, h4

                if (tagName === 'h2') {
                    if (currentH2) { // Close previous h2's ul if open
                        if (currentH3) {
                            tocHtml += '</ul>'; // Close h3 ul
                            currentH3 = null;
                        }
                        tocHtml += '</ul>'; // Close h2 ul
                    }
                    tocHtml += `<li><a href="#${headingId}">${headingText}</a><ul class="toc-h2-list">`;
                    currentH2 = headingText;
                    currentH3 = null; // Reset H3 for new H2
                } else if (tagName === 'h3') {
                    if (!currentH2) { // If an H3 appears before any H2, handle it (e.g., skip or add to main list)
                        // Optional: You could add it to the main list or skip
                        // For simplicity, we'll ensure it's nested under an H2 or add to top level if no H2
                         tocHtml += `<li><a href="#${headingId}">${headingText}</a></li>`;
                    } else {
                        if (!currentH3) {
                            tocHtml += `<ul class="toc-h3-list">`;
                            currentH3 = headingText;
                        }
                        tocHtml += `<li><a href="#${headingId}">${headingText}</a></li>`;
                    }
                } else if (tagName === 'h4') {
                    if (!currentH3) { // If H4 appears without H3, try to nest under H2, or add to top
                        if (!currentH2) {
                            tocHtml += `<li><a href="#${headingId}">${headingText}</a></li>`; // Add to main list if no H2/H3
                        } else {
                            tocHtml += `<ul class="toc-h3-list"><li><a href="#${headingId}">${headingText}</a></li></ul>`; // Create temporary H3 list
                        }
                    } else {
                         tocHtml += `<ul class="toc-h4-list"><li><a href="#${headingId}">${headingText}</a></li></ul>`; // H4s usually don't nest further
                    }
                }
            });

            // Close any open lists at the end
            if (currentH3) {
                tocHtml += '</ul>';
            }
            if (currentH2) {
                tocHtml += '</ul>';
            }
            tocHtml += '</ul>'; // Close the main list

            tocContainer.innerHTML = tocHtml;
        } else {
            tocContainer.style.display = 'none'; // Hide container if no headings found
        }
    }
});
//]]>
</script>
Key Points.
  • You may need to inspect your blog's HTML (right-click on a post, "Inspect" or "View Page Source") to find the exact class name or ID of the div that contains your main post content. If neither of these works for your theme, replace it with the correct selector (e.g., #main-post-content if your content div has an ID).
  • headings Selector: postBody.querySelectorAll('h2, h3, h4'); determines which heading levels will be included in your TOC. If you only use H2 and H3, you can remove h4.

Step 5: Test Your Automatic Table of Contents.

Go to any blog post where you've added the <div id="blog-toc-container"></div> placeholder and view it in your browser. The user can now easily navigate to different sections of the blog post.

⚠️ Troubleshoot

TOC not appearing? Double-check the postBody selector in the JavaScript matches your theme, and ensure the <div id="blog-toc-container"></div> is present in the post. Also, check for JavaScript errors in your browser's console (right-click, Inspect, Console tab).

Conclusion

The automatic Table of Contents is the clear winner for most modern websites and blogs for performance, efficiency, and sanity. It offers superior scalability, drastically reduces maintenance, and, when implemented correctly, can contribute positively to your page load speed. Invest a little time upfront in setting up a robust automatic TOC script, and you'll reap the performance and productivity benefits for years to come.
both, mystorymag

DON'T MISS

AI
© all rights reserved
made with by WorkWithG
Table of Contents