# Menu Highlighting

**URL:** <https://discourse.gohugo.io/t/menu-highlighting/35955>\
**Category:** support\
**Tags:** highlight\
**Created:** [December 8, 2021, 3:17pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955 "2021-12-08T15:17:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 8, 2021, 3:17pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/1 "2021-12-08T15:17:40Z")

</div>

Hey,  
I’m new to Hugo.  
I’m trying to keep the current page/section highlighted in the top menu.

I’ve tried the solutions suggested

1. [here](https://discourse.gohugo.io/t/highlight-current-menu-item-config-toml/22723)

2. [here](https://discourse.gohugo.io/t/highlight-active-top-level-menu-item/9827)

3. and here from stackoverflow :  
questions/48589485/highlighting-currentmenuitem-in-a-hugo-menu-that-is-defined-in-config-file

Then in **/layouts/partials/menu.html** I have tried the solutions suggested in the above links:

```auto
<!-- @ian copied this from:-->
<!-- https://stackoverflow.com/questions/48589485/highlighting-currentmenuitem-in-a-hugo-menu-that-is-defined-in-config-file -->
{{ $currentPage := . }}
{{ range .Site.Menus.main }}

    {{ $menu_item_url := .URL | relLangURL }}
    {{ $page_url:= $currentPage.RelPermalink | relLangURL }}

    {{ if eq $menu_item_url $page_url }}
        {{/* the menu item links to the current page (with relLangURL) */}}
    {{ end }}

{{ end }}

<!-- @ian copied this from:-->
<!-- https://discourse.gohugo.io/t/highlight-current-menu-item-config-toml/22723 -->

{{ $currentPage := . }}
{{ range .Site.Menus.main }}
<li>
    <a href="{{ .URL }}" 
        {{ if or ($currentPage.IsMenuCurrent "main" .) (eq $currentPage.Section .Identifier) }}
        class="active"
        {{ end }}
        {{ .Name }}</a>
</li>
{{ end }} 

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 8, 2021, 3:43pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/2 "2021-12-08T15:43:26Z")

</div>

See [https://github.com/gohugoio/hugo/issues/9150#issuecomment-965798975](https://github.com/gohugoio/hugo/issues/9150#issuecomment-965798975)

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 8, 2021, 5:22pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/3 "2021-12-08T17:22:51Z")

</div>

Ok,  
I change my config to be:

```auto
[[menu.primary]]
    name = "Home"
    pageref = "/"
    weight = 1
[[menu.primary]]
    name = "About"
    pageref = "/about/about/"
    weight = 2

```

Then inside **/layouts/partials/menu.html**

```auto
{{ $currentPage := . }}

{{ range .Site.Menus.main }}

<li class='menu-item{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} current-menu-item{{end}}'>

<a title="{{ .Name }}" href="{{ .pageref }}">{{ .Name }}</a>

</li>

{{ end }}

```

My menus stopped working completely?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 8, 2021, 5:29pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/4 "2021-12-08T17:29:29Z")

</div>

In site configuration you defined a menu named “primary”.

But in your partial you are ranging through a menu named “main”.

Which is it?

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 8, 2021, 5:58pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/5 "2021-12-08T17:58:35Z")

</div>

> [@jmooring](#):
>
> primary

Thanks Hugo.  
Changed them both do main. Didn’t seem to make a difference.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 8, 2021, 6:16pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/6 "2021-12-08T18:16:03Z")

</div>

Can you share your repository?

See [https://discourse.gohugo.io/t/requesting-help/9132](https://discourse.gohugo.io/t/requesting-help/9132).

> Let us see your code
> 
> Include a **link to the source code repository** of your project, because we really need the _context_ of seeing your templates and partials to be able to help you. It is trivial to do a quick `git clone` on your repo, then run `hugo server` in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.
> 
> If you can’t share your repository for whatever reason, consider creating a dummy repo that you _can_ share, which reproduces the problem you’re experiencing.

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 9, 2021, 6:32pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/7 "2021-12-09T18:32:17Z")

</div>

Its a private repository. I can add you?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 9, 2021, 7:39pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/8 "2021-12-09T19:39:43Z")

</div>

Sure.

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 10, 2021, 1:39pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/9 "2021-12-10T13:39:17Z")

</div>

Done!  
Thanks Jo.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 10, 2021, 6:25pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/10 "2021-12-10T18:25:37Z")

</div>

1) In your site configuration, you have placed `sectionPageMenu = "Main"` within the `[params]` table, so it is ignored. If you want to enable [this feature](https://gohugo.io/templates/menu-templates/#section-menu-for-lazy-bloggers), move that line into the root table (i.e., above all other tables).

2) When you specify `pageref` in a menu entry in your site configuration, it needs to match a page. When specifying a `pageref` for `content/About/About.md`, any of these will work:

```auto
pageref = "/About/about.md"
pageref = "/About/about"
pageref = "About/about.md"
pageref = "About/about"

```

What you currently have does not work because of the trailing slash:

```auto
pageref = "/about/about/"

```

3) Using uppercase characters in file and directory names is generally a bad idea. Use lowercase characters, and be consistent.

4) The the hugo-tanka theme ranges through the “primary” menu in its header:  
[https://github.com/nanxstats/hugo-tanka/blob/master/layouts/partials/header.html#L13](https://github.com/nanxstats/hugo-tanka/blob/master/layouts/partials/header.html#L13)

If you want to display that menu, modify the menu entries in your site configuration, change `[[menu.main]]` to `[[menu.primary]]` for all menu entries.

Or, if you want to use the “Section Menu for Lazy Bloggers”, make the change noted in #1 above, and change the menu name: `sectionPagesMenu = "primary"`

5) You created two templates in `layouts/partials/` but these are never called, which is good thing because both are broken.

- In `layouts/partials/menu.html` you tried to “comment out” a lot of template code by wrapping it within HTML comment tags (`<!-- -->`). [That doesn’t do what you think it does](https://gohugo.io/templates/introduction/#html-comments-containing-go-templates). Use [Go template comments](https://gohugo.io/templates/introduction/#go-templates-comments) instead.
- In `layouts/partials/sidebar.html` you have a complete HTML page (`DOCTYPE`, `<html>`, `<head>`, and `<body>` elements. No. A partial is a… partial.

I know your question was about highlighting active menu entries, but with the current state of your project, I really have no idea what you want.

- Do you want to use the header menu provided by theme, or write your own?
- Do you want menu entries determined automatically with the “Section Menu for Lazy Bloggers” feature, or do you want to manually define them in site configuration?
- Do you want the menu named “main” or “primary”?

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 13, 2021, 1:33pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/11 "2021-12-13T13:33:44Z")

</div>

Thank Joe, for taking the time to look over my, admittedly naive code.  
I answer to your questions:

> [@jmooring](#):
>
> - Do you want to use the header menu provided by theme, or write your own?

Use the themes for now.

> [@jmooring](#):
>
> - Do you want menu entries determined automatically with the “Section Menu for Lazy Bloggers”  
> feature, or do you want to manually define them in site configuration?

Sections menu should be enough for now.

> [@jmooring](#):
>
> - Do you want the menu named “main” or “primary”?

No preference.

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 13, 2021, 2:34pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/12 "2021-12-13T14:34:09Z")

</div>

Ok,  
I’ve made some additional changes:

- moved the "sectionPagesMenu = “primary” above the [params] in config.
- I went for the ‘lazy bloggers menu’ and (thanks to you) got it working.

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 16, 2021, 3:21pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/13 "2021-12-16T15:21:31Z")

</div>

@jmooring How do you call a template?

> Blockquote 5) You created two templates in `layouts/partials/` but these are never called,

---

<div class="post-metadata">

**Author:** ![itierney](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@itierney](https://discourse.gohugo.io/u/itierney)\
**Post date:** [December 20, 2021, 5:08pm UTC](https://discourse.gohugo.io/t/menu-highlighting/35955/14 "2021-12-20T17:08:12Z")

</div>

Ok,  
I got it working.

This code underlines the menu item:  
(From layouts/Partials/header.html)

```auto
        {{ if or ($.IsMenuCurrent "primary" .) (eq $ .Name ) }}
         <u>
          {{else}}
         </u>
        {{ end }}

```
