Navigation Menubar Example
Read This First
The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.
This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.
- There may be support gaps in some browser and assistive technology combinations, especially for mobile/touch devices. Testing code based on this example with assistive technologies is essential before considering use in production systems.
- The ARIA and Assistive Technologies Project is developing measurements of assistive technology support for APG examples.
- Robust accessibility can be further optimized by choosing implementation patterns that maximize use of semantic HTML and heeding the warning that No ARIA is better than Bad ARIA.
About This Example
Caution!
Before considering use of the Menubar Pattern for site navigation, it is important to understand:
- The
menubar
pattern requires complex functionality that is unnecessary for typical site navigation that is styled to look like a menubar with expandable sections orfly outs
. - A pattern more suited for typical site navigation with expandable groups of links is the Disclosure Pattern. For an example, see Example Disclosure Navigation Menu.
The following implementation of the Menubar Pattern demonstrates how a menubar can provide navigation menus.
The parent menu items in the menubar represent a section of a mythical university web site and open a submenu containing menu items that link to pages within that section.
The navigation system it illustrates is comparable to the navigation illustrated in the
Example Disclosure Navigation Menu.
As noted above, the disclosure pattern is better suited for most web sites because few sites need the additional keyboard functionality required to support the ARIA menubar
and menu
roles.
Similar examples include:
- Editor Menubar Example: Example of a menubar that presents text styling actions similar to an editor application.
- Example Disclosure Navigation Menu: Navigation menus implemented as simple drop down lists of links that are shown and hidden using disclosure buttons.
- Example Disclosure Navigation Menu with Top-Level Links
Example
Mythical University
Accessibility Features
-
Menu items that trigger navigation move focus to the target page title:
- An important aspect of designing a navigation menu experience is where keyboard focus moves when an item that triggers navigation is activated and the menu closes. If activating a menubar item changes content on the page without triggering a browser page load, i.e., works like typical single-page apps, the focus position after the content load significantly effects efficiency for keyboard and assistive technology users.
- This example behaves like a single page app and activating a menu item that loads new content moves focus to the beginning of the new content, which is a level one heading with content that matches the name of the activated menu item. Focusing on the heading informs screen reader users that navigation is complete and confirms the destination.
- To view other pages, keyboard users need to navigate back to the menubar . To optimize keyboard efficiency, this example locates the menubar immediately before the content display area in the Tab sequence.
- To help communicate that the arrow keys are available for directional navigation within the menubar and its submenus, a border is added to the menubar container when focus is within the menubar.
-
To help users locate the menu item that loads the currently displayed page, visual indicators of the path to that menu item are added to the menu items in the path:
- The parent menubar item in the path has an extra thick (4 pixel) bottom border.
- If the current page menu item is nested within a deeper submenu, the parent submenu item in the path has an extra thick (4 pixel) left side border.
- Finally, the menu item that loads the currently displayed page is styled with an extra thick border on the left side.
-
To support operating system high contrast settings:
- Because transparent borders are visible on some systems with operating system high contrast settings enabled, transparency cannot be used to create a visual difference between the element that is focused and other elements. Instead of using transparency, the focused element has a thicker border and less padding. When an element receives focus, its border changes from 0 to 2 pixels and padding is reduced by 2 pixels. When an element loses focus, its border changes from 2 to 0 pixels and padding is increased by 2 pixels.
-
To ensure the arrow icons used to indicate the expanded or collapsed state have sufficient contrast with the background when high contrast settings invert colors, the CSS
currentcolor
value for thefill
andstroke
properties of the SVGpolygon
element is used to synchronize the color with text content. If specific colors are used to specify thefill
andstroke
properties, these colors will remain the same in high contrast mode, which could lead to insufficient contrast between the icon and the background or even make the icon invisible if its color matches the high contrast mode background.
- Since the menubar presents a site navigation system, it is wrapped in a navigation region implemented with a
nav
element that has anaria-label
that matches the label on the menubar.
Keyboard Support
Menubar
Key | Function |
---|---|
Space Enter |
|
Right Arrow |
|
Left Arrow |
|
Down Arrow | Opens submenu and moves focus to first item in the submenu. |
Up Arrow | Opens submenu and moves focus to last item in the submenu. |
Home | Moves focus to first item in the menubar. |
End | Moves focus to last item in the menubar. |
Character |
|
Submenu
Key | Function |
---|---|
Space Enter |
|
Escape |
|
Right Arrow |
|
Left Arrow |
|
Down Arrow |
|
Up Arrow |
|
Home | Moves focus to the first item in the submenu. |
End | Moves focus to the last item in the submenu. |
Character |
|
Role, Property, State, and Tabindex Attributes
Landmarks
Role | Attribute | Element | Usage |
---|---|---|---|
banner |
header |
|
|
navigation |
nav |
Identifies the region containing the mythical university navigation. | |
aria-label="Mythical University" |
nav |
Provides an accessible name for the navigation landmark that describes the purpose of the navigation. |
|
region |
section |
|
|
aria-labelledby="idref" |
section |
Provides the region with an accessible name from the h1 element. |
|
contentinfo |
footer |
|
Menubar
Role | Attribute | Element | Usage |
---|---|---|---|
menubar
|
ul
|
|
|
aria-label="string"
|
ul
|
|
|
menuitem
|
a
|
|
|
tabindex="-1"
|
a
|
Makes the a element keyboard focusable, but not part of the tab sequence. |
|
tabindex="0"
|
a
|
|
|
aria-current="page"
|
a
|
|
|
aria-haspopup="true"
|
a
|
Indicates the menuitem has a submenu. | |
aria-expanded="true"
|
a
|
Indicates the submenu is open. | |
aria-expanded="false"
|
a
|
Indicates the submenu is closed. | |
none
|
li
|
|
Submenu
Role | Attribute | Element | Usage |
---|---|---|---|
menu
|
ul
|
Identifies the element as a menu container for a set of menu items. | |
aria-label="string"
|
ul
|
|
|
menuitem
|
a
|
|
|
tabindex="-1"
|
a
|
Keeps the a element focusable but removes it from the Tab sequence. |
|
aria-haspopup="true"
|
a
|
Indicates the menu item has a submenu. | |
aria-expanded="true"
|
a
|
Indicates the submenu is open. | |
aria-expanded="false"
|
a
|
Indicates the submenu is closed. | |
none
|
li
|
|
JavaScript and CSS Source Code
- CSS: menubar-navigation.css
- Javascript: menubar-navigation.js
HTML Source Code
To copy the following HTML code, please open it in CodePen.