--- title: "Highlighting the active menu item in Hugo" date: 2023-07-03T22:19:00-04:00 topics: ['hugo','web design'] --- I've recently been developing sites for some family/friends. I had one person request highlighting the currently selected menu item. I had built a custom theme which used a navbear defined in the site wide `hugo.toml`. There were a few different solutions but I wanted to highlight the solution I ended up using for my use case. A dummy of my site directory set up is: ``` content |-- section1 |--_index.md |--section1Post.md |-- section2 |--_index.md |--section2Post.md |-- section3 |--_index.md ``` In my header partial I created a menu by looping over the entries in `[[menu.main]]` defined in the `hugo.toml`. It's important here to end the `url` with a `/`. Hugo adds an ending `/`, to the urls of the pages that it creates. While this might not be necessary to make the hyperlink work, it will be necessary to get the highlighting correct. The definition of the menu looks like: ``` [menu] [[menu.main]] name= "section1" url= "/section1/" weight= "1" [[menu.main]] name= "section2" url= "/section2/" weight= "2" [[menu.main]] name= "section3" url= "/section3/" weight= "3" ``` The original header partial is pretty simple. I just loop over the range `.Site.Menus.main` and insert a `
` which is my div class for a navbar menu item. I populate the text and link from the variables associated with the main menu item. This is all wrapped within a `