Single-Page Anchor Link Navbar Template
A smart navigation bar specifically designed for single-page websites.
This type of navigation is essential for landing pages, product showcases, or any single-page website. The key is creating a smooth scrolling experience and providing clear visual feedback to the user, showing which section they are currently viewing.

The Single-Page Anchor Link Navbar
A smart navigation bar specifically designed for single-page websites. Instead of linking to other pages, the links smoothly scroll the user to different sections on the current page. The navbar also intelligently highlights the link corresponding to the section currently visible in the viewport.
- Smooth Scrolling — Built with pure CSS using
scroll-behavior: smooth
. Clicking a link in the navbar gracefully animates the scroll to the target section without requiring any JavaScript libraries. - Active Link Highlighting on Scroll — JavaScript observes which page section is currently visible and automatically applies an "active" style to the corresponding link in the navbar. This provides clear, real-time feedback to the user about their location on the page.
- Performant Intersection Observer API — The active link highlighting is achieved using the modern and highly performant
IntersectionObserver
API, which is much more efficient than traditional scroll event listeners for this task. - Clean and Responsive: The navbar is fully responsive, collapsing into a standard hamburger menu on mobile devices while maintaining all of its single-page functionality.
Coding Best Practices
- CSS custom properties (variables) are used for easy theme customization (colors, fonts).
- The code is appropriately commented to explain the purpose of each section.
- A tiny, efficient JavaScript snippet is used for the mobile menu toggle, as it is the most accessible and modern method (arguably superior to the older "checkbox hack").
Code
Here's the full code for the navbar template: