# Here is a way to do Table of Contents (ToC)

**URL:** <https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419>\
**Category:** Themes\
**Created:** [February 1, 2019, 7:14pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419 "2019-02-01T19:14:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MLFromScratch](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mlfromscratch/32/2278_2.png) [@MLFromScratch](https://forum.ghost.org/u/MLFromScratch)\
**Post date:** [February 1, 2019, 7:14pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/1 "2019-02-01T19:14:54Z")

</div>

After about 2 hours of fiddling with it and finding other peoples anwers, I got it to work (using jQuery).

**Steps:**

1. Add script at the bottom of this post in code injection in {{ghost\_foot}}
2. Use a **markdown card** to create a ToC like so (copy-paste this):

.

```
# Table of contents
1. [Go to Header](#header-name)

```

**EDIT (important):** You can also do it inline, if you want to link to something in one of your other posts or cross-reference in your current post. You would just highlight **_any word_** and click the link icon (or press CTRL+K), then insert as described in the next paragraph…

Whenever you create a header, you give that header a header name like **Header Name** , which translates to a link of _#header-name_. I hope it makes sense with the naming scheme, else feel free to comment.

```
<script>
(function(document, history, location) {
  var HISTORY_SUPPORT = !!(history && history.pushState);

  var anchorScrolls = {
    ANCHOR_REGEX: /^#[^]+$/,
    OFFSET_HEIGHT_PX: 70,

    /**
     * Establish events, and fix initial scroll position if a hash is provided.
     */
    init: function() {
      this.scrollToCurrent();
      $(window).on('hashchange', $.proxy(this, 'scrollToCurrent'));
      $('body').on('click', 'a', $.proxy(this, 'delegateAnchors'));
    },

    /**
     * Return the offset amount to deduct from the normal scroll position.
     * Modify as appropriate to allow for dynamic calculations
     */
    getFixedOffset: function() {
      return this.OFFSET_HEIGHT_PX;
    },

    /**
     * If the provided href is an anchor which resolves to an element on the
     * page, scroll to it.
     * @param {String} href
     * @return {Boolean} - Was the href an anchor.
     */
    scrollIfAnchor: function(href, pushToHistory) {
      var match, anchorOffset;

      if(!this.ANCHOR_REGEX.test(href)) {
        return false;
      }

      match = document.getElementById(href.slice(1));

      if(match) {
        anchorOffset = $(match).offset().top - this.getFixedOffset();
        $('html, body').animate({ scrollTop: anchorOffset});

        // Add the state to history as-per normal anchor links
        if(HISTORY_SUPPORT && pushToHistory) {
          history.pushState({}, document.title, location.pathname + href);
        }
      }

      return !!match;
    },
    
    /**
     * Attempt to scroll to the current location's hash.
     */
    scrollToCurrent: function(e) { 
      if(this.scrollIfAnchor(window.location.hash) && e) {
      	e.preventDefault();
      }
    },

    /**
     * If the click event's target was an anchor, fix the scroll position.
     */
    delegateAnchors: function(e) {
      var elem = e.target;

      if(this.scrollIfAnchor(elem.getAttribute('href'), true)) {
        e.preventDefault();
      }
    }
  };
	$(window).on( "load", $.proxy(anchorScrolls, 'init'));
	//$(document).ready($.proxy(anchorScrolls, 'init'));
})(window.document, window.history, window.location);
</script>
```

---

<div class="post-metadata">

**Author:** ![ClementineRaynor](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@ClementineRaynor](https://forum.ghost.org/u/ClementineRaynor)\
**Post date:** [March 5, 2019, 10:02am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/2 "2019-03-05T10:02:09Z")

</div>

This is great. Thanks for sharing this

---

<div class="post-metadata">

**Author:** ![MLFromScratch](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mlfromscratch/32/2278_2.png) [@MLFromScratch](https://forum.ghost.org/u/MLFromScratch)\
**Post date:** [March 6, 2019, 1:10am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/3 "2019-03-06T01:10:32Z")

</div>

I had this request for the longest time. It really works wonders and pretty smoothly.  
Let me know if there is anything else you need to know about this ToC 🙂

---

<div class="post-metadata">

**Author:** ![MLFromScratch](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mlfromscratch/32/2278_2.png) [@MLFromScratch](https://forum.ghost.org/u/MLFromScratch)\
**Post date:** [April 4, 2019, 4:36pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/4 "2019-04-04T16:36:44Z")

</div>

I found a bug, where you could not click on the subscribe button on the front page. Here is the updated script

```
<script>
(function(document, history, location) {
  var HISTORY_SUPPORT = !!(history && history.pushState);

  var anchorScrolls = {
    ANCHOR_REGEX: /^#[^]+$/,
    OFFSET_HEIGHT_PX: 130,

    /**
     * Establish events, and fix initial scroll position if a hash is provided.
     */
    init: function() {
      this.scrollToCurrent();
      $(window).on('hashchange', $.proxy(this, 'scrollToCurrent'));
      $('body').on('click', 'a', $.proxy(this, 'delegateAnchors'));
    },

    /**
     * Return the offset amount to deduct from the normal scroll position.
     * Modify as appropriate to allow for dynamic calculations
     */
    getFixedOffset: function() {
      return this.OFFSET_HEIGHT_PX;
    },

    /**
     * If the provided href is an anchor which resolves to an element on the
     * page, scroll to it.
     * @param {String} href
     * @return {Boolean} - Was the href an anchor.
     */
    scrollIfAnchor: function(href, pushToHistory) {
      var match, anchorOffset;

      if(!this.ANCHOR_REGEX.test(href)) {
        return false;
      }

      match = document.getElementById(href.slice(1));

      if(match) {
        anchorOffset = $(match).offset().top - this.getFixedOffset();
        $('html, body').animate({ scrollTop: anchorOffset});

        // Add the state to history as-per normal anchor links
        if(HISTORY_SUPPORT && pushToHistory) {
          history.pushState({}, document.title, location.pathname + href);
        }
      }

      return !!match;
    },
    
    /**
     * Attempt to scroll to the current location's hash.
     */
    scrollToCurrent: function(e) { 
      if(this.scrollIfAnchor(window.location.hash) && e) {
      	e.preventDefault();
      }
    },

    /**
     * If the click event's target was an anchor, fix the scroll position.
     */
    delegateAnchors: function(e) {
      var elem = e.target;
      if(elem.getAttribute('href') != '#subscribe'){
          if(this.scrollIfAnchor(elem.getAttribute('href'), true)) {
        		e.preventDefault();
      	  	}
      }
    }
  };
	$(window).on( "load", $.proxy(anchorScrolls, 'init'));
	//$(document).ready($.proxy(anchorScrolls, 'init'));
})(window.document, window.history, window.location);
</script>
```

---

<div class="post-metadata">

**Author:** ![Brandon\_Skerritt](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/brandon_skerritt/32/3101_2.png) [@Brandon\_Skerritt](https://forum.ghost.org/u/Brandon_Skerritt)\
**Post date:** [June 3, 2019, 9:29pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/5 "2019-06-03T21:29:45Z")

</div>

Hey,  
Do I need to install JQuery for this to work?

---

<div class="post-metadata">

**Author:** ![MLFromScratch](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mlfromscratch/32/2278_2.png) [@MLFromScratch](https://forum.ghost.org/u/MLFromScratch)\
**Post date:** [June 3, 2019, 10:31pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/6 "2019-06-03T22:31:45Z")

</div>

Nope. Just follow the steps and you will be fine. If you want a preview of it, you can visit my website. [Here is a post that you can try it out](https://mlfromscratch.com/random-forest-gridsearchcv-python/)

---

<div class="post-metadata">

**Author:** ![Brandon\_Skerritt](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/brandon_skerritt/32/3101_2.png) [@Brandon\_Skerritt](https://forum.ghost.org/u/Brandon_Skerritt)\
**Post date:** [June 4, 2019, 5:50pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/7 "2019-06-04T17:50:22Z")

</div>

Brilliant, thanks!!!  
Just a small note that I didn’t know: caps matters 🙂

---

<div class="post-metadata">

**Author:** ![axle07](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/axle07/32/3615_2.png) [@axle07](https://forum.ghost.org/u/axle07)\
**Post date:** [July 10, 2019, 7:05pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/8 "2019-07-10T19:05:41Z")

</div>

It seems that the generated id is in fact “headername” rather than “header-name”.

---

<div class="post-metadata">

**Author:** ![MLFromScratch](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mlfromscratch/32/2278_2.png) [@MLFromScratch](https://forum.ghost.org/u/MLFromScratch)\
**Post date:** [November 10, 2019, 2:36pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/10 "2019-11-10T14:36:53Z")

</div>

The ID is generated by Ghost core, so you have to contact them or do a pull request on github yourself

---

<div class="post-metadata">

**Author:** ![JREAM](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/jream/32/5216_2.png) [@JREAM](https://forum.ghost.org/u/JREAM)\
**Post date:** [November 23, 2019, 4:20am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/11 "2019-11-23T04:20:35Z")

</div>

Awesome thanks! Also if you use VSCode grab **Markdown All in One** then you can generate the ToC [VS Code](https://code.visualstudio.com) (`ctrl + shift + p` and type `toc` and Insert it.

---

<div class="post-metadata">

**Author:** ![doithuong](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/doithuong/32/5524_2.png) [@doithuong](https://forum.ghost.org/u/doithuong)\
**Post date:** [December 18, 2019, 5:32pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/12 "2019-12-18T17:32:35Z")

</div>

This is great. Thanks for sharing this

---

<div class="post-metadata">

**Author:** ![tylerbarton](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@tylerbarton](https://forum.ghost.org/u/tylerbarton)\
**Post date:** [January 5, 2020, 10:30pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/13 "2020-01-05T22:30:40Z")

</div>

This works great in general, however when I use the code inject for the footer it puts a TOC on the home page as well as the posts except for adding the card at the bottom of the page. I tried you second code that should eliminate the bottom card but it actually did nothing in my theme. No TOC at all. I haven’t figure out a way to get it to ignore the main landing page yet. If anyone does please let us know or if I can get it figured out I’ll post it.

I don’t want to edit the html of each post page as that can get out of hand quickly on a large site.

---

<div class="post-metadata">

**Author:** ![tylerbarton](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@tylerbarton](https://forum.ghost.org/u/tylerbarton)\
**Post date:** [January 7, 2020, 12:59am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/14 "2020-01-07T00:59:02Z")

</div>

Sorry, I got confused on who’s code I was using. I actually used [Add a dynamic Table of Contents to a Ghost blog](https://hooshmand.net/quick-hack-add-a-dynamic-table-of-contents-to-a-ghost-blog/)

And I finally did get it to work correctly once I read it right and placed the code at the bottom of the post in a HTML card.

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [February 17, 2020, 2:21pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/15 "2020-02-17T14:21:24Z")

</div>

Hey all 👋, we’ve recently put together a new tutorial to show you how to add a Table Of Contents to your Ghost site. Take a look, hope this helps:  
[https://ghost.org/tutorials/adding-a-table-of-contents/](https://ghost.org/tutorials/adding-a-table-of-contents/)

PS. If you have any feedback let us know!

---

<div class="post-metadata">

**Author:** ![gregpozo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/gregpozo/32/3395_2.png) [@gregpozo](https://forum.ghost.org/u/gregpozo)\
**Post date:** [February 17, 2020, 2:30pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/16 "2020-02-17T14:30:30Z")

</div>

Very good tutorial indeed! Thanks for the insight.

---

<div class="post-metadata">

**Author:** ![theodorechu](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@theodorechu](https://forum.ghost.org/u/theodorechu)\
**Post date:** [February 23, 2020, 9:02am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/17 "2020-02-23T09:02:37Z")

</div>

This is very cool. Thank you for working on this.

I noticed that the sticky table of contents shifts the text to the left and widens the text box, so there are more words per line. Is there a way to keep the text centered and the text box the same width? I think keeping the text in the center and having fewer words per line makes the content easier to read.

My testing with sticky TOC ([compare with the Lyra Demo without TOC](https://lyra.ghost.io/publishing-options/)):

 ![image](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/2X/1/190b23562084fa7f337666a0ae879ec33989f7f8.png)

For non-sticky TOC, the h3 headers add an extra line in the TOC and only appear under the first h2:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/2X/e/ea0847f7ee34c3754e06502adc99b58e24935740.png)

---

<div class="post-metadata">

**Author:** ![kairos](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/kairos/32/4752_2.png) [@kairos](https://forum.ghost.org/u/kairos)\
**Post date:** [February 23, 2020, 4:21pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/18 "2020-02-23T16:21:10Z")

</div>

How does one retain the current form of content being centered and occupying let’s say about 40% width, while still having the sticky TOC on the side?

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [February 24, 2020, 9:07am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/20 "2020-02-24T09:07:46Z")

</div>

@theodorechu @kairos This is all possible by adjusting the values in the CSS. The example provided in the tutorial is designed to get you started. Changing the values for the `.post-full-content` CSS from the ones in the tutorial ([https://ghost.org/tutorials/adding-a-table-of-contents/#advanced-styling](https://ghost.org/tutorials/adding-a-table-of-contents/#advanced-styling)) is a good place to start 🙂

---

<div class="post-metadata">

**Author:** ![theodorechu](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@theodorechu](https://forum.ghost.org/u/theodorechu)\
**Post date:** [February 24, 2020, 10:26pm UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/21 "2020-02-24T22:26:28Z")

</div>

OK, thanks @DavidDarnes. I looked at the advanced styling, and I noticed a problem. Here’s some feedback:

On Casper/Lyra, the `max-width` of `.inner` is `1040px`, and `.post-full-content` is inside `.inner`, so the `max-width` of `.post-full-content` is also `1040px`.

Suppose we want to preserve the width of `.post-content` at the default `700px`.

The `min-width` of the sticky TOC according to the advanced styling tutorial is `260px`. We probably don’t want to make this any smaller.

The sticky TOC is on the right side of `.post-content`, so we can center the `.post-content` column by adding another column on the left side of `.post-content`. We can also assign a `min-width` of `260px`.

The width of the three columns together is `260px` + `700px` + `260px` = `1220px`. This is greater than the width allowed by `.inner`, so the columns are off-center.

The result is the following:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/2X/5/5ebdd02bce9e9f32e057738fe0e04ebe22de6678.png)

Do you have any suggestions on how to fix this?

Do you have any suggestions on how to fix the problem of the h3 headers not showing in the TOC?

---

<div class="post-metadata">

**Author:** ![denvergeeks](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/denvergeeks/32/7401_2.png) [@denvergeeks](https://forum.ghost.org/u/denvergeeks)\
**Post date:** [February 25, 2020, 1:08am UTC](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419/22 "2020-02-25T01:08:44Z")

</div>

@theodorechu are you using CSS **float: right** for the div containing the TOC?

[Next page](https://forum.ghost.org/t/here-is-a-way-to-do-table-of-contents-toc/5419.md?page=2)
