How to Build a Responsive H-Menu: Step-by-Step Guide
What an H-Menu is
An H-Menu (horizontal menu) displays primary navigation items in a single row across the top of a page, often with dropdowns for submenus. It’s common in desktop layouts and should adapt for smaller screens.
Step-by-step build (assumes HTML/CSS/JS)
- HTML structure
- Use a semanticwith an unordered list (
- ) of links ().
[blocked]
[blocked]
[blocked] - Use a semanticwith an unordered list (
Example snippets
HTML:
html
<nav> <button aria-controls=“main-menu” aria-expanded=“false”>Menu</button> <ul id=“main-menu” role=“menubar”> <li role=“none”><a role=“menuitem” href=”/home”>Home</a></li> <li role=“none”> <button aria-haspopup=“true” aria-expanded=“false”>Products</button> <ul role=“menu”> <li><a role=“menuitem” href=”/p1”>Product 1</a></li> </ul> </li> </ul></nav>
CSS (core):
css
nav ul { display:flex; gap:1rem; list-style:none; margin:0; padding:0; }nav li { position:relative; }nav ul ul { position:absolute; display:none; }nav li:focus-within > ul,nav li:hover > ul { display:block; }
JS (concept):
js
// Toggle mobile menubtn.addEventListener(‘click’, ()=> { const expanded = btn.getAttribute(‘aria-expanded’) === ‘true’; btn.setAttribute(‘aria-expanded’, String(!expanded)); menu.hidden = expanded;});
Testing checklist
- Keyboard-only navigation works.
- Screen reader announces menu structure and expanded/collapsed states.
- Mobile toggle functions and layout adapts.
- Visual contrast and touch target sizes meet accessibility guidelines.
- Behavior graceful if JavaScript is disabled (progressive enhancement).
When to use an H-Menu
- p]:inline” data-streamdown=“list-item”>When top real estate allows visible navigation.
Leave a Reply