Delanie Tipp graphic design and web development logo

Modern CSS Navigation Highlight

Technical documentation on creating smooth animated navigation highlights using CSS transforms and JavaScript

What & Why

This technique creates a smooth animated highlight bar that moves between navigation links using CSS transforms and transitions. Traditionally, navigation hover effects were built by changing background colors on each link, which often felt abrupt and visually inconsistent. By animating a single highlight element instead, we achieve smoother motion, better performance, and a more polished user experience.

How It Works

The highlight is a separate <span> element positioned absolutely within the nav container. When hovering over a link, JavaScript calculates the link's width and position, then animates the highlight to match using CSS transforms. This creates a fluid sliding effect.

Code Snippet

/* CSS Transition */
.highlight {
  transition: left 0.4s cubic-bezier(.4,0,.2,1),
              width 0.4s cubic-bezier(.4,0,.2,1);
}

/* JavaScript Logic - FIXED */
link.addEventListener("mouseenter", () => {
  const linkRect = link.getBoundingClientRect();
  const navRect = nav.getBoundingClientRect();
  const leftPosition = linkRect.left - navRect.left;
  
  highlight.style.width = `${linkRect.width}px`;
  highlight.style.left = `${leftPosition}px`;
});

/* Mobile - Hide highlight */
@media (max-width: 767px) {
  .highlight {
    display: none;
  }
}

Key Benefits

Live Demo

Hover over the navigation links above to see the highlight animation in action. The highlight smoothly follows your cursor and resizes to match each link. On mobile, the highlight is automatically hidden since hover states don't exist on touch devices.

Gotchas & Considerations

Resource

Inspired by Smashing Magazine: Creating a Moving Highlight Navigation Bar with JavaScript and CSS

← Back to Portfolio