Technology | Axel Ebert - Mechanical Engineer (Dipl. Ing. Maschinenbau)

Responsive Dropdown Menu with Pure CSS

Classic dropdown menus open on :hover. A touchscreen has no hover, so on a smartphone such menus are hard or impossible to use. This version works on every device: on small screens it shows a burger button and an accordion list, on wide screens a horizontal bar with dropdowns. Submenus open on click or tap, so mouse, touch and keyboard all behave the same way. It needs no JavaScript.

Resize your browser window to see both layouts.

Example 1: Simple Navigation

Example 2: Mega Menu in Columns

For menus with many entries. On wide screens the submenu opens as a panel with several columns, on a smartphone the same markup becomes a plain list. Only the extra class dd-mega on the <nav> element is needed.

How It Works

  • Submenus are <details> elements. Browsers open and close them on click, tap or keyboard (Enter / Space) without any script. Screen readers announce them correctly.
  • Only one submenu stays open. All <details> of one menu share the same name attribute (name="dd1"), so opening one closes the others. Use a different name for each menu on a page.
  • The burger button is a hidden checkbox. A <label> toggles it and the CSS shows or hides the list with :checked. Every menu needs its own unique id / for pair (dd-toggle-1, dd-toggle-2 ...).
  • One media query switches the layout. From 52em (about 830 px) the list becomes a horizontal bar. Change this value if your site needs another breakpoint.
  • Colours are CSS variables at the top of the stylesheet. Background gradient image is done by CSS (--dd-bg-top to --dd-bg), so no image file is needed.
  • Keyboard focus is visible and the animation respects prefers-reduced-motion.
  • Requires a current browser. The name attribute on <details> works in all major browsers since 2024. In older ones the menu still works, but several submenus can be open at the same time.

Code

Entry for the <head> section:

<link rel="stylesheet" href="../css/drop-down-tutorial/navigation.css" type="text/css">

HTML structure (a menu entry without submenu is simply <li><a href="...">Text</a></li>):

<nav class="dd" aria-label="Main menu">
  <input type="checkbox" id="dd-toggle-1" class="dd-toggle">
  <label for="dd-toggle-1" class="dd-burger">Menu</label>
  <ul class="dd-list">
    <li>
      <details name="dd1">
        <summary>Description</summary>
        <ul class="dd-panel">
          <li><a href="#features">Features</a></li>
          <li><a href="#validation">Validation</a></li>
        </ul>
      </details>
    </li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>

Mega menu: add dd-mega to the <nav> and group the links inside the panel:

<ul class="dd-panel">
  <li class="dd-group"><span class="dd-heading">Articles</span>
    <ul>
      <li><a href="...">First article</a></li>
    </ul>
  </li>
  <li class="dd-group dd-group--wide"> ... </li>   <!-- long list in two columns -->
</ul>

CSS navigation.css

/* ==========================================================
   Responsive Dropdown Navigation - pure CSS, no JavaScript needed
   Mobile first: burger menu with accordion
   Desktop (from 52em): horizontal bar with click-to-open dropdowns
   Variant: add class "dd-mega" to the <nav> for a multi-column panel
   ========================================================== */

.dd {
  /* Colours - change these to restyle the whole menu */
  --dd-bg: #426bc6;
  --dd-bg-top: #6485cf;
  --dd-light: #bdcaed;
  --dd-text: #fff;
  --dd-dark: #000;
  --dd-focus: #ffd54a;

  position: relative;
  z-index: 50;
  background: linear-gradient(var(--dd-bg-top), var(--dd-bg));
  border-block: 1px solid #333;
  line-height: 1.3;
}
/* Menue mit geoeffnetem Untermenue liegt vor den anderen Menues auf der Seite */
.dd:focus-within { z-index: 60; }
.dd:has(details[open]) { z-index: 60; }

.dd ul { list-style: none; margin: 0; padding: 0; }
.dd li { margin: 0; padding: 0; }   /* ueberschreibt li-Einrueckung der Seiten-Stylesheets */
.dd a, .dd summary { color: var(--dd-text); text-decoration: none; }
.dd img { vertical-align: -3px; margin-right: 4px; }

.dd a:focus-visible,
.dd summary:focus-visible { outline: 3px solid var(--dd-focus); outline-offset: -3px; }

/* ---------- Burger button (checkbox hack) ---------- */
.dd-toggle {
  position: absolute; top: 0; left: 0;
  width: 1px; height: 1px; opacity: 0; pointer-events: none;
}
.dd-burger {
  display: flex; align-items: center; gap: .6em;
  padding: 12px 16px; color: var(--dd-text);
  font-weight: bold; cursor: pointer; user-select: none;
}
.dd-burger::before { content: "\2630"; font-size: 1.4em; line-height: 1; }
.dd-toggle:checked + .dd-burger::before { content: "\2715"; }
.dd-toggle:focus-visible + .dd-burger { outline: 3px solid var(--dd-focus); outline-offset: -3px; }

/* ---------- Mobile: vertical list ---------- */
.dd-list { display: none; }
.dd-toggle:checked ~ .dd-list { display: block; }
.dd-list > li { border-top: 1px solid rgba(255, 255, 255, .25); }

.dd-list > li > a,
.dd summary { display: block; padding: 12px 16px; cursor: pointer; }

.dd summary {
  display: flex; align-items: center; justify-content: space-between;
  list-style: none;
}
.dd summary::-webkit-details-marker { display: none; }
.dd summary::after { content: "\25BE"; margin-left: .6em; }
.dd details[open] > summary::after { transform: rotate(180deg); }

.dd-list > li > a:hover,
.dd-list > li > a:focus,
.dd summary:hover,
.dd details[open] > summary { background: var(--dd-light); color: var(--dd-dark); }

/* Submenu panel */
.dd-panel { background: var(--dd-light); }
.dd-panel a { display: block; padding: 10px 16px 10px 32px; color: var(--dd-dark); }
.dd-panel a:hover,
.dd-panel a:focus { background: var(--dd-bg); color: var(--dd-text); }
.dd-heading {
  display: block; padding: 10px 16px 4px;
  font-weight: bold; color: var(--dd-dark);
}

@media (prefers-reduced-motion: no-preference) {
  .dd summary::after { transition: transform .15s; }
}

/* ---------- Desktop: horizontal bar ---------- */
@media (min-width: 52em) {
  .dd-burger { display: none; }

  .dd-list,
  .dd-toggle:checked ~ .dd-list { display: flex; flex-wrap: wrap; }

  .dd-list > li { position: relative; border-top: 0; }

  .dd-panel {
    position: absolute; top: 100%; left: 0; z-index: 100;
    min-width: 220px;
    box-shadow: 0 6px 12px rgba(0, 0, 0, .3);
  }
  .dd-panel a { padding: 6px 16px; }
  .dd-heading { padding: 8px 16px 2px; }

  /* ----- Mega menu: panel spans the whole bar, content in columns ----- */
  .dd-mega .dd-list > li { position: static; }
  .dd-mega .dd-panel {
    left: 0; right: 0; min-width: 0;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 8px 16px;
    padding: 12px 16px 16px;
  }
  .dd-mega .dd-panel a { padding: 4px 8px; }
  .dd-mega .dd-heading {
    padding: 0 8px 6px; margin-bottom: 4px;
    border-bottom: 1px solid rgba(0, 0, 0, .35);
  }
  /* Long group: spans two grid columns and flows into two text columns */
  .dd-mega .dd-group--wide { grid-column: span 2; }
  .dd-mega .dd-group--wide > ul { column-count: 2; column-gap: 16px; }
  .dd-mega .dd-group--wide li { break-inside: avoid; }
}

Optional JavaScript dropdown.js, loaded at the end of the page with <script src="../js/dropdown.js" defer></script>. The menu itself does not need it: opening and closing is done by the browser (<details>) and the burger button by CSS. The script only adds one convenience for desktop browsers. Without it an open dropdown stays open until you click its title again or open another one. With it, a click anywhere else or the Escape key closes the dropdown, as you would expect from a menu.

document.addEventListener('click', function (e) {
  document.querySelectorAll('.dd details[open]').forEach(function (d) {
    if (!d.contains(e.target)) d.open = false;
  });
});
document.addEventListener('keydown', function (e) {
  if (e.key === 'Escape') {
    document.querySelectorAll('.dd details[open]').forEach(function (d) { d.open = false; });
  }
});

Overview – Übersicht