Back to the blog Tvorba webu

Build an Accessible Horizontal Dropdown Menu with HTML and CSS

Build a horizontal navigation menu with semantic HTML and responsive CSS. Adapt a native disclosure menu, then test its keyboard, pointer, and touch interactions before publishing.

Horizontal website navigation with an open submenu, shown alongside a stacked mobile layout

Choose the interaction before writing the CSS

A dropdown menu can give visitors an overview of a site’s hierarchy, but it assumes they can predict which heading contains the page they need. Keep important destinations findable through clear labels and other routes on the site; do not make a hover-only submenu the sole way to reach them.

For a no-script menu that works across desktop and mobile, a native disclosure using details and summary is a practical starting point. The browser provides its open-and-close behavior, including activation by click or by focusing the summary and pressing Space. The example below uses this approach.

Choose the interaction before writing the CSS

Build a meaningful navigation structure

Put the navigation inside a nav landmark and represent its items as a list. Website navigation usually has no required sequence, so an unordered list is generally appropriate; use an ordered list when the order itself conveys a sequence. This structure helps assistive technologies identify the group and communicate how many items it contains.

Give the navigation a short, descriptive label when that will help distinguish it from other navigation areas, such as a footer menu. For a disclosure-style submenu, use a concise summary for the group of pages it reveals and place those links in a nested list. If the parent heading must also lead to a page, provide a separate link alongside the disclosure control. Mark the current page with aria-current="page" where appropriate.

Build a meaningful navigation structure

Add the HTML for the menu

This example uses illustrative paths; replace them with the destinations on your own site. The summary controls the submenu, while the other top-level items are ordinary links.

```html <nav aria-label="Main navigation"> <ul> <li><a href="/about/" aria-current="page">About</a></li> <li> <details> <summary>Services</summary> <ul> <li><a href="/services/consulting/">Consulting</a></li> <li><a href="/services/training/">Training</a></li> </ul> </details> </li> <li><a href="/contact/">Contact</a></li> </ul> </nav> ```

The browser manages whether the disclosure is open. Its contents are visible when it is open and hidden when it is closed, so do not add a fixed aria-expanded value that would not track the changing state. You can style the open state with details[open].

Style the desktop menu and adapt it for mobile

The following CSS lays out the top-level items in a row, positions an open submenu beneath its summary, and changes the layout at a sample narrow-screen breakpoint. Adjust the breakpoint to suit your labels and available space. The focus outline is kept visible, and the arrow changes direction when the disclosure opens.

```css nav > ul { display: flex; gap: 1.5rem; list-style: none; margin: 0; padding: 0; } nav li { position: relative; } nav a, nav summary { display: block; padding: 0.75rem; } nav summary { cursor: pointer; list-style: none; } nav summary::-webkit-details-marker { display: none; } nav summary::after { content: " ▾"; } nav details[open] > summary::after { content: " ▴"; } nav a:focus-visible, nav summary:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; } nav details > ul { position: absolute; left: 0; top: 100%; z-index: 1; min-width: 12rem; list-style: none; margin: 0; padding: 0.25rem; background: white; box-shadow: 0 0.25rem 0.75rem rgb(0 0 0 / 15%); } nav details > ul a { display: block; } @media (max-width: 48rem) { nav > ul { flex-direction: column; gap: 0; } nav details > ul { position: static; min-width: 0; box-shadow: none; } } ```

The submenu is positioned beneath its summary on wide screens and returns to the document flow on narrow screens. Avoid clipping it with a fixed-height container. Keep the same wording, order, and destinations as the layout changes, and check that the open panel does not obscure essential content.

Understand the limits of a CSS-only hover menu

A conventional CSS flyout can hide a nested list and reveal it on :hover. Adding :focus-within can reveal it when the parent or one of its descendants has focus, but it may also open the submenu during Tab navigation. Keyboard users may then have to move through every child link before reaching the next main item.

CSS alone does not provide the scripted toggle behavior needed to avoid that trade-off while synchronizing an expanded-state attribute. If your requirements call for a custom keyboard sequence, delayed closing, or synchronized ARIA state, CSS-only is not a complete solution. Choose a native disclosure or use an appropriately implemented script. If a parent item must both navigate and reveal children, use separate controls for the link and the disclosure.

Test the menu before publishing

Check that the navigation is identified and labeled appropriately, that each link leads to the expected page, and that the current page is marked where appropriate. Use Tab to reach the summary, activate it with the keyboard, and move through the revealed links. Confirm that focus remains visible and that you can reach the other main items.

Then test with a pointer and touch, including at narrow viewport widths. Try the longest real labels and the deepest submenu you plan to use. Look for clipped content, unclear controls, and destinations that are difficult to find. If the hierarchy or keyboard path is confusing, simplify the menu or change the interaction before publishing.

Frequently asked questions

Can I make a dropdown menu accessible with only HTML and CSS?

You can build a no-script menu using semantic navigation, lists, and native details and summary disclosures. Test the complete interaction with a keyboard, pointer, and touch, and provide other routes to important child pages.

Does :focus-within make a hover submenu keyboard-accessible?

It can reveal a submenu when an element inside it receives focus, but it may also open during Tab navigation. Keyboard users may have to pass through every child link before reaching the next main item.

How should a mobile submenu open without JavaScript?

A details element with a descriptive summary provides a native disclosure that can be activated by touch or by focusing it and pressing Space. Use CSS to arrange the navigation vertically on narrow screens, and check that its links remain visible and operable.

Sources and further reading 7