Contents

Modify the font of the navigation bar in Hugo

The cover image is a comparison between the before and after modifications.

Introduction

When building a website, you may notice that the default font is not very appealing. This article will use Hugo as the static site generator and introduce how to change the navigation bar font to your preferred one.

Finding a Font

There are many websites offering fonts online. In this guide, we will use Google fonts to embed fonts into the website

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/modify%20the%20font%20of%20the%20navigation%20bar%20in%20hugo/google%20fonts%20homepage.png
The homepage of Google fonts.

Selecting a Font

First, visit Google fonts and choose a font you like. For example, I selected LXGW WenKai TC. Follow step one in the image below and click the Get font button.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/modify%20the%20font%20of%20the%20navigation%20bar%20in%20hugo/LXGW%20WenKai%20TC's%20page.png
The LXGW WenKai TC font page on Google fonts.

Obtaining the Embed Code

In step two, click the <> Get embed code button.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/modify%20the%20font%20of%20the%20navigation%20bar%20in%20hugo/select%20fonts%20to%20embed.png
The page for selecting embed code or downloading the font.

Next, follow the instructions provided by Google fonts to embed the code into your website.

In step three, switch to the @import option.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/modify%20the%20font%20of%20the%20navigation%20bar%20in%20hugo/embed%20codes.png
The example page provided by Google fonts for embedding code.

Remember the code from steps four and five, as we will use it later.

Embedding the Font

Adjusting the Configuration

Next, locate the hugo.toml or config.toml file in the website root directory and find [params]. Add the following information.

1
2
3
4
5
6
7
8
[params]
  [params.page]
    [params.page.library]
      [params.page.library.css]
        # located in "assets/"
        # Or
        # someCSS = "https://cdn.example.com/some.css"
        customCSS = "/css/custom.css" 
Note
The [params] parameter may vary depending on the Hugo theme and version. Please adjust accordingly.

Adding the custom.css File

Navigate to the /assets/css/ directory in the website root and create a custom.css file. Then, add the code from steps four and five in #Obtaining the Embed Code.

1
2
3
4
5
6
7
/* Add LXGW WenKai TC font */
@import url('https://fonts.googleapis.com/css2?family=LXGW+WenKai+TC&display=swap');
.lxgw-wenkai-tc-regular {
    font-family: "LXGW WenKai TC", cursive;
    font-weight: 400;
    font-style: normal;
  }
Tip
You can change customCSS = "/css/custom.css" in hugo.toml or config.toml to any term of your choice. However, ensure that the term is consistent in both the configuration file and the filename in /assets/css/.

At this point, the font has been successfully embedded into the website.

Changing the Navigation Bar Font

Viewing the Page Source Code

Here, we use Google Chrome to demonstrate how to extract the page source code. Use the appropriate method for your browser.

On any page of the website, right-click to open the menu and select View Page Source.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/modify%20the%20font%20of%20the%20navigation%20bar%20in%20hugo/check%20the%20source%20code%20of%20the%20page.png
The method to view the page source in the Chrome browser.

Find the text matching the navigation bar text and note the value of the class attribute before it. In the example below, the class attribute for the navigation bar text is menu-item.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/modify%20the%20font%20of%20the%20navigation%20bar%20in%20hugo/view%20source.en.png
Viewing the source code and identifying the `class` attribute value of the navigation bar text.

Tip
Use the browser’s search function to quickly locate keywords. Pressing Ctrl + F usually opens the page search.

Modifying the custom.css File

Return to the /assets/css/custom.css file and add the following code to force the navigation bar font change.

1
2
3
4
/* Change font for navigation bar (Articles, Tags, Categories, About) */
.menu-item { 
    font-family: "LXGW WenKai TC", cursive !important;
}

Remember to replace menu-item and "LXGW WenKai TC" with your class attribute value and font choice. Note that menu-item is preceded by a period (.), which should not be omitted.

The complete code will look like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/* Add LXGW WenKai TC font */
@import url('https://fonts.googleapis.com/css2?family=LXGW+WenKai+TC&display=swap');
.lxgw-wenkai-tc-regular {
    font-family: "LXGW WenKai TC", cursive;
    font-weight: 400;
    font-style: normal;
  }

/* Change font for navigation bar (Articles, Tags, Categories, About) */
.menu-item { 
    font-family: "LXGW WenKai TC", cursive !important;
}

Modifying the link.html File

Finally, navigate to the /layouts/partials/head/ directory in the website root, locate the link.html file, and add the following code at the end to complete the font change for the navigation bar.

1
2
3
{{- /* custom.css */ -}}
{{ $customCSS := resources.Get "css/custom.css" | minify | fingerprint }}
<link rel="stylesheet" href="{{ $customCSS.Permalink }}">
Note
Ensure that all instances of custom match the filename set in #Adjusting the Configuration.

Conclusion

Modifying a website’s font may seem like a small change, but it can significantly enhance the site’s visual appeal, making the overall design better suited to personal style and branding. By selecting the right font, a website can become more attractive and improve the user browsing experience. Additionally, we can assign specific fonts to different elements, adding more depth to the site’s design. Hopefully, this guide will help you successfully change the font of your website’s navigation bar and create a webpage that reflects your unique style!

Environment

  • Hugo 0.144.2
  • LoveIt theme (version from GitHub on February 21, 2025)

References