Responsive Navigation Menu
This example demonstrates how to create a responsive website navigation menu using HTML, CSS, and a small amount of JavaScript. The navigation displays horizontally on larger screens and changes to a collapsible menu on smaller screens.
Try It: Resize the browser window to 768 pixels or less. The horizontal navigation changes to a Menu button. Select the button to open or close the navigation links. You can also use the Tab, Enter, and Space keys to test keyboard accessibility.
Home
Welcome to the responsive navigation example. This section represents the homepage content.
The navigation menu provides links to different sections of the page, allowing visitors to move between them quickly.
About
This section can provide information about a website, business, or organization.
The responsive navigation menu ensures that visitors can access important links on both large and small screens.
Services
This section can describe the services or products offered by a business.
On smaller screens, the navigation links remain hidden until the visitor opens the menu.
Portfolio
This section can display projects, design examples, or other featured work.
The menu button provides a compact navigation interface without taking up unnecessary screen space.
Contact
This section can contain contact information, a contact form, or other communication options.
Selecting a navigation link on smaller screens automatically closes the menu after the link is activated.
How Responsive Navigation Works
The HTML header element contains the website branding, menu button, and navigation links.
The nav element identifies the main navigation area, while an unordered list organizes the individual links.
CSS Flexbox arranges the branding and navigation horizontally on larger screens.
The following declaration controls the main header layout:
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
The justify-content: space-between declaration distributes the available space between the branding and navigation.
The flex-wrap: wrap declaration allows elements to move onto additional rows when necessary.
Mobile Navigation Menu
A CSS media query changes the navigation layout when the viewport width is 768 pixels or less.
At smaller screen sizes, the horizontal navigation is hidden and the Menu button becomes visible.
When the button is selected, JavaScript adds the is-open class to the navigation element.
The following CSS displays the expanded navigation:
.responsive-navigation.is-open {
display: block;
}
The navigation links are arranged vertically using flex-direction: column, making them easier to select on smaller screens.
JavaScript Menu Functionality
JavaScript controls whether the mobile navigation is expanded or collapsed.
The classList.toggle() method adds or removes the is-open class when the Menu button is selected.
The aria-expanded attribute is updated to indicate the current state of the navigation menu.
When a navigation link is selected, the menu closes automatically on smaller screens.
The Escape key can also close the menu and return keyboard focus to the Menu button.
Navigation Accessibility
The Menu button uses the HTML button element, allowing visitors to activate it using a mouse, keyboard, or assistive technology.
The aria-controls attribute identifies the navigation element controlled by the button.
The aria-expanded attribute communicates whether the mobile menu is open or closed.
The aria-label attribute provides an accessible name for the navigation region and menu button.
Visible keyboard focus indicators help visitors identify the currently focused navigation item.
Customizing Responsive Navigation
You can change the background colors, text colors, padding, and typography to match your website's design.
Modify the 768px media query breakpoint to control when the navigation changes from horizontal to collapsible.
You can also replace the website branding text with a logo or other identifying content.
To add additional navigation links, duplicate an existing li element and update its destination and text.
When using this example on a multi-page website, replace the demonstration fragment links with the appropriate webpage URLs and use aria-current="page" on the link representing the current page.