Active Links in Menu

I am trying to set an active class on the current page menu item. I have my menu setup in config.toml

    name = "Home"
    weight = 1
    identifier = "home"
    url = "/"
    name = "Projects"
    weight = 2
    identifier = "projects"
    url = "/projects/"

In my navigation.html partial I have a conditional

    {{ $currentPage := . }}
    {{ range .Site.Menus.main }}
      <a class="nav-links{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} active{{end}}" href="{{.URL}}">{{ .Name }}</a>
    {{ end }}

However, the first item in the menu “Home” is highlighted and it gets the class of active, but when I click on any other page, those links don’t get the active class. The home does lose the class when it’s not active.

I am still confused, the documentation isn’t that clear on Hugo. I would like to understand this better.


I got it, the front matter had to be updated instead of putting them in the config file

This works for me (the classes come from Tachyons):

<nav id="main-nav">

  {{ $page := .Page }}
  {{ $site := .Site }}

  <!-- -->
  {{ $list := .Site.Menus.main }}
  {{ $len := (len $list) }}

    <!-- All pages but the homepage shall have a link to homepage -->
    {{ if not .IsHome }}
      <li class="dib lh-copy"><a class="link underline-hover dim br ph2" href="/">Home</a></li>
    {{ end }}

    {{ range $index, $element := $list }}
      {{ $is := $page.IsMenuCurrent "main" . }}
      {{ $has := $page.HasMenuCurrent "main" . }}
        {{ if or $is $has }}
          class="li-active dib lh-copy"
        {{ else }}
          class="dib lh-copy"
        {{ end }}>
          {{ if eq (add $index 1) $len }}
            class="ph2 link underline-hover dim"
          {{ else }}
            <!-- Here it sets a border right unless its last element -->
            class="ph2 link underline-hover dim br"
          {{ end }}
            href="{{ .URL | absURL }}">
          {{ .Pre }} {{ .Name }}
    {{ end }}

And in Front Matter the menu entries look like:

name = Nav title
weight = 25

weight = 10


Sorry, but if isn’t in config.toml so where exactly should menu be?

Actually it’s a choice, I am still discovering Hugo. If I don’t mention anything in the config file and just add some front matter in pages like menu: “main”, weight: “2” for example, it still shows up in the top main navigation :slight_smile:

You can put in in config.toml or in Front Matter the same way. Both are discovered by .Site.Menus.main. And you can do both…

Thanks. Can you provide some full example? Because now I’m using in config.toml it renders ok with nested items.
When I put menu item in frontmatter it displays well but I have issues with nested items and checking what item is active now.

In config.toml:

  name = "About"
  weight = -110
  url = "/about"
  identifier = "about"

    name = "Other"
    weight = -110
    identifier = "other"

    name = "Culture"
    weight = -110
    url = "/other/culture"
    parent = "other"

    name = "Contacts"
    weight = -110
    url = "/other/contacts"
    parent = "other"

    name = "FAQ"
    weight = -110
    url = "/other/faq"
    parent = "other"

Changing active from here doesn’t work

What should I do to make it works? Any suggestion would be great, because my is over for now :frowning:

Perhaps it helps if you browse through my test project:

Look in partials/site/header.html where you find the code for the menu. In content you can see the Markdown Files with Front Matter Menus.

Most of it is in german but you’ll find your way.

Hope this helps.

Thank you for help. I hope too I’ll find answers

I used JavaScript for this, in case someone looks for the same query I was searching and jumps on this thread:

(function() {
    const links = document.getElementsByTagName('a')
    const currentUrl = location.href
    for (const link of links) {
        if (link.href === currentUrl) {

It’s possible to do this without JavaScript which is (in my eyes) a better solution, for the following reasons:

  • visitors that have JavaScript turned off will still see the correct result
  • because JavaScript can sometimes be executed later (for network or other reasons) the user will first see the menu without the highlighted current link and then see that link light up once the JavaScript runs.