Tutorial: CSS-Only Sticky Slide-In Navbar

by Hugo Vale Pereira

HTML

<!-- From:  https://medium.com/@TusharKanjariya/this-navbar-trick-needs-no-js-1c91c9a8b7a6-->

<!-- NAVBAR -->
<header id="navbar">
  <div class="nav-inner">
    <span class="nav-logo">CSS.tricks</span>
    <ul class="nav-links">
      <li><a href="#how">How it works</a></li>
      <li><a href="#code">The code</a></li>
      <li><a href="#support">Browser support</a></li>
    </ul>
    <a href="https://medium.com/@TusharKanjariya" target="_blank" class="nav-cta">Medium</a>
  </div>
</header>

<!-- HERO -->
<section class="hero">
  <span class="hero-badge">Sticky Navbar · No JS Needed</span>
  <h1 class="hero-title">
    A navbar that <em>slides in</em> — zero JS required
  </h1>
  <p class="hero-sub">
    Scroll down to see it appear. Powered entirely by CSS scroll-state detection.
  </p>
  <div class="scroll-hint">
    <span>Scroll to trigger</span>
    <div class="scroll-arrow"></div>
  </div>
</section>

<hr class="divider" />

<!-- HOW IT WORKS -->
<div class="section" id="how">
  <p class="section-label">The trick</p>
  <h2>How this works with no JavaScript</h2>

  <div class="notice">
    <div class="notice-dot"></div>
    <p>The navbar above just slid in as you scrolled — using <strong>CSS scroll-state detection</strong>. No
      event listeners. No class toggling. Here's how.</p>
  </div>

  <p>
    Modern CSS can detect the exact moment a sticky element becomes "stuck." When your header locks to the top
    of the viewport, CSS notices — and you can style it differently from that point on.
  </p>
  <p>
    No scroll listener firing on every tick. No manual state. Just the browser's layout engine doing what it was
    built to do.
  </p>

  <div class="steps">
    <div class="step">
      <div class="step-num">01</div>
      <div>
        <h3>Make the header sticky</h3>
        <p>position: sticky + top: 0. The browser now internally tracks when it enters its "stuck" state.
        </p>
     ...

CSS

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0a0a0f;
  --surface: #111118;
  --border: rgba(255, 255, 255, 0.08);
  --accent: #c8f04a;
  --accent-dim: rgba(200, 240, 74, 0.1);
  --text: #e8e8e0;
  --muted: #6b6b72;
}

html {
  scroll-behavior: smooth;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Instrument Sans", sans-serif;
  line-height: 1.65;
  overflow-x: hidden;
}

/* ── NAVBAR ── */
header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;

  /* Hidden by default */
  transform: translateY(-100%);
  opacity: 0;
  transition: transform 0.5s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s ease;
}

/* Class added by JS when scrolled past hero */
header.is-stuck {
  transform: translateY(0);
  opacity: 1;
}

.nav-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2.5rem;
  height: 64px;
  background: rgba(10, 10, 15, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

.nav-logo {
  font-family: "DM Mono", monospace;
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
}

.nav-links {
  display: flex;
  gap: 2rem;
  list-style: none;
}

.nav-links a {
  text-decoration: none;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 500;
  transition: color 0.2s;
}

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

.nav-cta {
  font-family: "DM Mono", monospace;
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--bg);
  background: var(--accent);
  padding: 0.45rem 1.1rem;
  border-radius: 4px;
  text-decoration: none;
  transition: opacity 0.2s;
}

.nav-cta:hover {
  opacity: 0.8;
}

/* ── HERO ── */
.hero {
  min-height:...

JavaScript

// 4-line JS fallback for environments that don't support scroll-state
window.addEventListener("scroll", () => {
  document
    .getElementById("navbar")
    .classList.toggle("is-stuck", window.scrollY > 80);
});