NAV001

Sticky Navigation

A modern navigation bar that sticks to the top of the page when scrolling

JavaScript
Font Awesome Included: This snippet uses Font Awesome icons. The required stylesheet is automatically included at the top of the code block. If you are going to use this snippet in your project, add the stylesheet to your <head>.

Live Preview

Code

HTML
<!-- Font Awesome Icons; Make sure to add it in the <head> tag -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  
<!-- Sticky Navigation -->
<header class="sticky-nav">
  <div class="sticky-nav-container">
    <div class="sticky-nav-logo">
      <i class="fas fa-mountain"></i>
      <span>StickyNav</span>
    </div>
    <nav class="sticky-nav-menu">
      <ul>
        <li><a href="#" class="active">Home</a></li>
        <li><a href="#">Features</a></li>
        <li><a href="#">Pricing</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
    <button class="sticky-nav-toggle">
      <i class="fas fa-bars"></i>
    </button>
  </div>
</header>
<div class="content">
  <!-- Your page content goes here -->
</div>
CSS
:root {
  --nav-height: 70px;
  --nav-bg: #ffffff;
  --nav-bg-scrolled: #ffffff;
  --nav-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  --nav-text: #333333;
  --nav-text-hover: #4a6cf7;
  --nav-transition: all 0.3s ease;
  --nav-logo-size: 1.5rem;
  --nav-active-indicator: 2px solid #4a6cf7;
}

/* Dark mode variables */
@media (prefers-color-scheme: dark) {
  :root {
    --nav-bg: #121212;
    --nav-bg-scrolled: #1a1a1a;
    --nav-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    --nav-text: #e1e1e1;
    --nav-text-hover: #6c8fff;
    --nav-active-indicator: 2px solid #6c8fff;
  }
}

.sticky-nav {
  position: sticky;
  top: 0;
  width: 100%;
  height: var(--nav-height);
  background-color: var(--nav-bg);
  transition: var(--nav-transition);
  z-index: 1000;
}

.sticky-nav.scrolled {
  background-color: var(--nav-bg-scrolled);
  box-shadow: var(--nav-shadow);
}

.sticky-nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sticky-nav-logo {
  display: flex;
  align-items: center;
  font-size: var(--nav-logo-size);
  font-weight: 700;
  color: var(--nav-text);
}

.sticky-nav-logo i {
  margin-right: 0.5rem;
  color: var(--nav-text-hover);
}

.sticky-nav-menu ul {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.sticky-nav-menu li {
  margin: 0 1rem;
}

.sticky-nav-menu a {
  display: block;
  padding: 0.5rem 0;
  color: var(--nav-text);
  text-decoration: none;
  font-weight: 500;
  position: relative;
  transition: var(--nav-transition);
}

.sticky-nav-menu a::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: var(--nav-text-hover);
  transition: var(--nav-transition);
}

.sticky-nav-menu a:hover {
  color: var(--nav-text-hover);
}

.sticky-nav-menu a:hover::after,
.sticky-nav-menu a.active::after {
  width: 100%;
}

.sticky-nav-menu a.active {
  color: var(--nav-text-hover);
}

.sticky-nav-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--nav-text);
  font-size: 1.5rem;
  cursor: pointer;
  transition: var(--nav-transition);
}

.sticky-nav-toggle:hover {
  color: var(--nav-text-hover);
}

/* Mobile responsiveness */
@media (max-width: 768px) {
  .sticky-nav-menu {
    position: fixed;
    top: var(--nav-height);
    left: 0;
    width: 100%;
    background-color: var(--nav-bg-scrolled);
    box-shadow: var(--nav-shadow);
    padding: 1rem 0;
    clip-path: polygon(0 0, 100% 0, 100% 0, 0 0);
    transition: var(--nav-transition);
  }
  
  .sticky-nav-menu.mobile-active {
    clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
  }
  
  .sticky-nav-menu ul {
    flex-direction: column;
    align-items: center;
  }
  
  .sticky-nav-menu li {
    margin: 0.5rem 0;
    width: 100%;
    text-align: center;
  }
  
  .sticky-nav-menu a {
    padding: 0.75rem 0;
  }
  
  .sticky-nav-toggle {
    display: block;
  }
}
JavaScript
// Wait for DOM to be fully loaded
document.addEventListener('DOMContentLoaded', function() {
  // Get the navigation element
  const nav = document.querySelector('.sticky-nav');
  const menuToggle = document.querySelector('.sticky-nav-toggle');
  const menu = document.querySelector('.sticky-nav-menu');
  
  // Scroll event listener
  window.addEventListener('scroll', function() {
    // Add 'scrolled' class when page is scrolled
    if (window.scrollY > 50) {
      nav.classList.add('scrolled');
    } else {
      nav.classList.remove('scrolled');
    }
  });
  
  // Mobile menu toggle
  if (menuToggle) {
    menuToggle.addEventListener('click', function() {
      menu.classList.toggle('mobile-active');
      // Toggle icon between bars and times (X)
      const icon = menuToggle.querySelector('i');
      if (icon.classList.contains('fa-bars')) {
        icon.classList.remove('fa-bars');
        icon.classList.add('fa-times');
      } else {
        icon.classList.remove('fa-times');
        icon.classList.add('fa-bars');
      }
    });
  }
  
  // Handle clicks on menu links (for mobile)
  const menuLinks = document.querySelectorAll('.sticky-nav-menu a');
  menuLinks.forEach(link => {
    link.addEventListener('click', function() {
      // If current viewport is mobile size
      if (window.innerWidth <= 768) {
        menu.classList.remove('mobile-active');
        const icon = menuToggle.querySelector('i');
        icon.classList.remove('fa-times');
        icon.classList.add('fa-bars');
      }
      
      // Remove active class from all links
      menuLinks.forEach(item => item.classList.remove('active'));
      // Add active class to clicked link
      this.classList.add('active');
    });
  });
});

// Resize event handler to reset mobile menu
window.addEventListener('resize', function() {
  const menu = document.querySelector('.sticky-nav-menu');
  const menuToggle = document.querySelector('.sticky-nav-toggle');
  const icon = menuToggle?.querySelector('i');
  
  if (window.innerWidth > 768) {
    menu?.classList.remove('mobile-active');
    if (icon && icon.classList.contains('fa-times')) {
      icon.classList.remove('fa-times');
      icon.classList.add('fa-bars');
    }
  }
});