Highlight the current page in Jekyll navigation
Highlighting the current page makes a navigation menu easier to scan. In Jekyll, you only need a data file and a small Liquid condition.
Create _data/navigation.yml and add the navigation labels and URLs:
- text: About
url: /about/
- text: Projects
url: /projects/
- text: Work
url: /work/
Then loop through the items in your navigation template. When an item’s URL matches page.url, add the active class:
{% for nav in site.data.navigation %}
<li{% if nav.url == page.url %} class="active"{% endif %}>
<a href="{{ nav.url }}">{{ nav.text }}</a>
</li>
{% endfor %}
Now style the active link:
li.active a {
color: lightgray;
cursor: default;
}
The match must be exact, including leading and trailing slashes. If the class does not appear, temporarily print {{ page.url }} on the page and compare it with the URL in navigation.yml.
This keeps the navigation in one data file and lets Jekyll handle the current-page state without JavaScript.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- The HTTP Status Codes I Actually Check
A practical guide to common HTTP response codes, redirects, client and server errors, and the headers that explain them.
- What Makes a Website a Progressive Web App?
A practical look at service workers, web app manifests, installation, and the cases where a Progressive Web App is worth building.
- What a Static Site Generator Actually Does
A static site generator combines content and templates before deployment, producing HTML files that a web server can deliver directly.