Site icon Ampersand Tutorials

Materialize CSS Navbar: Responsive Menus Made Simple

Quick answer: A Materialize navbar has two parts — a nav wrapper holding a nav-wrapper (logo left, links right) and a separate hamburger trigger with data-target that opens a sidenav on mobile. The responsive behavior is built in: links inside hide-on-med-and-down disappear below 992px while the hamburger appears, so the same markup serves desktop and phone. The complete working navbar — with dropdown menus, mobile drawer and initialization — is below in copy-paste form; the usual failure (dead hamburger) is a missing JS init, fixed in the last section.

The complete working navbar

<nav>
  <div class="nav-wrapper light-blue darken-2">
    <a href="#" class="brand-logo left">MySite</a>
    <ul class="right hide-on-med-and-down">
      <li><a href="#">Home</a></li>
      <li><a href="#">Courses</a></li>
      <li><a href="#">About</a></li>
    </ul>
    <a href="#" data-target="mobile-demo" class="sidenav-trigger right">
      <i class="material-icons">menu</i>
    </a>
  </div>
</nav>

<ul class="sidenav" id="mobile-demo">
  <li><a href="#">Home</a></li>
  <li><a href="#">Courses</a></li>
  <li><a href="#">About</a></li>
</ul>

<script>
  document.addEventListener('DOMContentLoaded', function () {
    M.Sidenav.init(document.querySelectorAll('.sidenav'));
    M.Dropdown.init(document.querySelectorAll('.dropdown-trigger'));
  });
</script>

Read the structure once and every Materialize navbar variation becomes a tweak: the nav-wrapper is the inner content bar (the nav element itself carries the background through its child), brand-logo positions the site name, and the two lists — desktop links with hide-on-med-and-down, mobile sidenav with matching data-target — cover every screen. If this is your first Materialize component, the Materialize CSS tutorial builds the base page this navbar sits on.

Position and height options

GoalHowNotes
Fixed top (default)Nothing — nav is fixed automaticallyBody gets padding-top from the framework
Sticky only after scrollnav-wrapper + scrollspy classNav starts in flow, sticks later
Transparent over a heroadd transparent + box-shadow-noneAdd shadow back on scroll via JS toggle
Taller navOverride nav height line-height and paddingFull recipe in the height guide below
Bottom nav (mobile apps)Tab demo pattern: fixed bottom ulIcon + label rows, 5 items max

Height is the most-requested tweak: Materialize’s nav is tuned to 64px desktop / 56px mobile, and changing it means adjusting the line-height on the wrapper together with the body padding offset — the complete before-and-after is in the increase navbar height post. The default placement behavior and logo alignment rules are covered in the navbar guide that opens this series’ navigation pair.

One accessibility habit worth building now: give the hamburger trigger an aria-label describing what it opens, and keep every nav link a real anchor with an href — Materialize does not add these for you. The framework handles focus trapping inside the open sidenav, but the labels and destinations are yours to provide.

Dropdown menus inside the navbar

Dropdowns are an anchor with a dropdown-trigger class, an id, and a sibling ul.dropdown-content — hover on desktop, tap on mobile:

<a class="dropdown-trigger btn-flat" href="#" data-target="courses-dd">
  Courses <i class="material-icons right">arrow_drop_down</i>
</a>
<ul id="courses-dd" class="dropdown-content">
  <li><a href="#">Materialize</a></li>
  <li><a href="#">Bootstrap</a></li>
  <li class="divider"></li>
  <li><a href="#">All courses</a></li>
</ul>

Then init it: M.Dropdown.init(document.querySelectorAll('.dropdown-trigger')). If dropdowns must open on click instead of hover (the common mobile-usability fix), pass { constrainWidth: false, hover: false } as options. For nested sub-menus inside the mobile drawer rather than the top bar, use the accordion submenu pattern — it keeps deep trees scrollable on small screens.

Troubleshooting the dead hamburger

Those same four culprits cover the standalone sidenav, which has its own complete sidenav guide — read that one if your drawer fails outside a navbar.

Building a real site header and want it reviewed — logo scale, breakpoints, dropdown accessibility? Ampersand Academy teaches Materialize front-end one-to-one, on your own project.

Frequently asked questions

Why is my Materialize navbar hamburger not working?

The Materialize JavaScript is missing or M.Sidenav.init() never ran. Also check that the trigger data-target exactly matches the sidenav ul id, and that you are not calling the old 0.97.x jQuery sideNav syntax.

How do I make the Materialize navbar transparent?

Add the transparent and box-shadow-none classes to the nav element. For the standard pattern where the shadow returns after scrolling, toggle those classes from a scroll listener past roughly 50 pixels.

How do I center the logo in the navbar?

Use class brand-logo without left or right positioning — it centers by default. Because it is absolutely positioned, add horizontal padding on the nav-wrapper so long link groups never overlap it.

How do I hide navbar links on mobile?

Add hide-on-med-and-down to the desktop link list and include a sidenav-trigger with data-target for the mobile list. Below 992 pixels the framework hides the list and shows the hamburger automatically.

How do I change the navbar height in Materialize?

Override the line-height on the nav wrapper together with its padding, then keep the body top padding in sync so content does not hide behind the taller bar. The height guide walks through the exact declarations.

Exit mobile version