A white polar bear

Nice Polar Blog

Categories: [Guides] [Posts]

Tags: [Web-dev] [Development]

A nice table of contents

Until now, I have used the following snippet to make a table of contents.

<div class="toc">
  <p>Table of Contents</p>

  {{- .Page.TableOfContents }}
</div>

However, when you have a lot of sections it becomes a bit annoying for the visitor to scroll to read.

An easy solution would be to use JS to detect a click and expand the table, or using CSS :hover events but in each case you will add unnecessary complexity as the project grows.

Another alternative is just putting the table at the sides of the article, like on Josh Comeau website.

Nonetheless on this post I discovered maybe a hidden gem of HTML5: the <details> tag. This tag has great browser support and for such it would not make sense to not want to use it on any new project.

The new code now looks like this:

<details>
  <summary>Table of Contents</summary>

{{- .Page.TableOfContents }}

</details>

On the CSS side I would add these little changes:

summary {
  user-select: none;
}

summary:hover {
  cursor: pointer;
}

Previous: VS Code privacy settings

Next: A quick change to featured posts in Hugo

This work is licensed under CC BY-SA 4.0 A circle with the letters CC in the middle A circle with the shape of a human in the middle A circle with an arrow circling to point itself