Skip to content

Navigation Bar

How to make a accessibility-friendly responsive navigation bar using HTML, CSS and JavaScript.

Building a Responsive Navigation Bar

A responsive navigation bar is essential for the majority of sites.

Navigation bar header example

Below is a working example of the navigation bar. On large screen it shows all navigation links directly, on smaller screens it hides them behind a hamburger menu.

So how can you build this?

The HTML, CSS and JavaScript

HTML

The HTML for a navigation bar that contains a logo and a group of navigation links. The button with the aria-controls is used to toggle the hamburger navigation view on the mobile screen. The hamburger and close icon are not shown on the desktop screen. In the mobile view only one of them is shown, depending on the toggle state, more on that in the CSS section.

The visually hidden span containing Menu is used for people with screen readers.

<header>
<a href="#" class="logo-icon">
<img src="logo.png" alt="Logo icon">
</a>
<button aria-controls="primary-navigation" aria-expanded="false" class="menu-toggle-button">
<span class="visually-hidden">Menu</span>
<img src="hamburger.svg" alt="Hamburger icon" class="hamburger-icon" >
<img src="close.svg" alt="Close icon" class="close-icon">
</button>
<nav id="primary-navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Something</a></li>
</ul>
</nav>
</header>

It is also possible to build a navigation bar without JavaScript, but since these can’t control the aria labels their accessability is worse.


Sources & References