FINDING YOUR DIRECTION

0%
← Scrollhaus Journal

Motion Implementation · Published · 5 min read

Animated Navbar: Responsive CSS, Mobile Menu and Keyboard Support

Give navigation useful motion without making visitors wait. Build a responsive navbar with clear link feedback, an explicit mobile menu state and a stable keyboard path.

Published by Amanjot Singh, founder of Scrollhaus. AI-assisted editorial work; product context, sources and corrections are documented in our editorial methodology.

Original Scrollhaus diagram of a desktop navbar connected to an expanded mobile menu
Scrollhaus Journal · Motion Implementation

Quick answer: an animated navbar adds restrained movement to navigation: a link underline, a short menu entrance or clear active-page feedback. Start with normal HTML links and a real button for a collapsed menu. The animation should help visitors find the next page; it should never delay or hide the route they need.

Try the working responsive navbar demo. It has hover and keyboard-focus feedback, a mobile disclosure button, Escape handling and a reduced-motion state. The code is a standalone starter, not a replacement for testing your project's navigation.

Which navbar animation should you use?

PatternUseful whenWhat to avoid
Underline on hover and focusA visitor is choosing among a few named pagesFeedback that appears only with a mouse
Short mobile menu entranceLinks collapse beneath a Menu buttonA long reveal before a destination can be selected
Active-page markerVisitors need to know where they areUsing motion or colour as the only state indicator
Sticky navigationA long page needs quick access to its main sectionsCovering anchored headings or keyboard focus

For a portfolio, label destinations around the work: Projects, About and Contact. For a product page, use destinations that support a decision: Features, Examples and Pricing. Avoid making the entire header drift, tilt or disappear whenever someone reverses scroll direction. Before choosing a layout, compare the portfolio website examples and animated hero patterns.

Build the navigation before adding motion

This example keeps links visible when JavaScript is unavailable. Enhancement hides them only on small screens, then exposes a button with an expanded state. The list remains in normal page flow rather than an overlay that needs a focus trap. In a site with several navigation landmarks, give each one a useful distinct label.

<nav class="demo-nav" aria-label="Primary navigation">
  <a class="brand" href="/">Your brand</a>
  <button class="nav-toggle" type="button"
    aria-expanded="false" aria-controls="demo-links" hidden>Menu</button>
  <ul class="nav-links" id="demo-links">
    <li><a href="/work/">Projects</a></li>
    <li><a href="/about/">About</a></li>
    <li><a href="/contact/">Contact</a></li>
  </ul>
</nav>

Replace the example URLs with real pages before publishing. On the current destination, add aria-current="page" to that link; do not mark every link as current. A typical website navigation list does not need role="menu". That role implies a different keyboard interaction model, as the W3C disclosure navigation example explains.

Add a CSS underline that also responds to focus

.nav-links a { position: relative; display: block;
  padding: 8px 0; text-decoration: none; }
.nav-links a::after { content: ""; position: absolute;
  left: 0; bottom: 2px; width: 100%; height: 2px;
  background: currentColor; transform: scaleX(0);
  transform-origin: left; transition: transform 180ms ease; }
.nav-links a:hover::after,
.nav-links a:focus-visible::after,
.nav-links a[aria-current="page"]::after { transform: scaleX(1); }
.nav-links a:focus-visible { outline: 3px solid currentColor;
  outline-offset: 5px; }
@media (prefers-reduced-motion: reduce) {
  .nav-links a::after { transition: none; }
}

The pseudo-element changes visually without moving the surrounding links. Leave the focus outline in place: an animated underline is supplemental feedback, not an excuse to remove the browser's keyboard indicator. The CSS text-animation examples use the same idea for editorial links and headings.

Make the mobile menu state explicit

const nav=document.querySelector('.demo-nav');
const toggle=document.querySelector('.nav-toggle');
const links=document.querySelector('#demo-links');
const narrow=matchMedia('(max-width:640px)');
function setOpen(open){links.hidden=!open;toggle.setAttribute('aria-expanded',String(open));}
function syncLayout(){
  const focusInside=links.contains(document.activeElement);
  toggle.hidden=!narrow.matches;
  if(narrow.matches){setOpen(focusInside);}
  else{if(document.activeElement===toggle)links.querySelector('a').focus();setOpen(true);}
}
syncLayout();narrow.addEventListener('change',syncLayout);
toggle.addEventListener('click',()=>setOpen(links.hidden));
nav.addEventListener('keydown',event=>{
  if(event.key==='Escape'&&narrow.matches&&!links.hidden){setOpen(false);toggle.focus();}
});
links.addEventListener('click',event=>{
  if(narrow.matches&&event.target.closest('a')){setOpen(false);toggle.focus();}
});

The live example includes the matching responsive CSS. When the layout crosses the mobile breakpoint, it keeps the list open if a link has focus. When it expands to desktop while the toggle has focus, it moves focus to the first link. Closed links use the hidden attribute, so keyboard users cannot tab into an invisible list. Closing happens immediately; the opening list has a short entrance. Do not keep a hidden menu tabbable merely to preserve its exit animation.

In React, store the open state in your component and register breakpoint and keyboard listeners in an effect with cleanup. Reuse your router's link component where appropriate. Give multiple navbars unique control IDs instead of copying the same demo-links ID into every header.

Keep navigation separate from the cinematic hero

A video or 3D scene can occupy the hero while the navbar remains a stable layer above it. Check the link contrast against the brightest frame, not only the poster. Keep decorative canvas layers from intercepting pointer input. A header with a solid or predictable background is easier to read across changing footage. The 3D animated background guide covers those choices.

If you use a sticky header with in-page links, reserve space with scroll-margin-top on target sections. Test deep links and focused controls too. For a whole-page motion system, use the scroll-animation performance checklist before adding more header effects.

An AI-builder brief for this navbar

Build a responsive navigation landmark with real links and a labelled Menu button. Keep links available without JavaScript. Use a transform underline for hover and visible keyboard focus, an explicit mobile expanded state, Escape-to-close with focus return, and a reduced-motion fallback. Preserve visible text and do not add a full-screen overlay or scroll-hiding header. Explain how it connects to my routes.

Use that brief with your approved brand content and a working preview. Explore Scrollhaus design templates for layout direction, motion effects for individual interactions, or the AI website build guide for the complete workflow. Check the source and dependencies available for the particular entry you choose.

Test the animated navbar before launch

Source references: W3C navigation disclosure, MDN reduced motion and MDN transforms.

Frequently asked questions

Can I build an animated navbar with CSS only?

CSS handles underline feedback and entrance motion. This example uses JavaScript for the responsive disclosure state, Escape handling and focus return.

Should website navigation use the ARIA menu role?

Usually a navigation landmark with ordinary links and disclosure buttons is appropriate. An ARIA menu implies a different keyboard interaction model.

Does the mobile navbar work without JavaScript?

The starter keeps links visible before enhancement. If JavaScript is unavailable, visitors can still reach each destination.

Browse Scrollhaus website designs and build resources →

Need help with a template, account, or project? Email admin@scrollhaus.ai.