dynamic-sticky-sidebardynamic-sticky-sidebar
Guide
  • Vanilla JS
  • React
  • Vue
API
Demo
npm
  • English
  • فارسی
  • العربية
  • Deutsch
  • Français
  • Italiano
GitHub
Guide
  • Vanilla JS
  • React
  • Vue
API
Demo
npm
  • English
  • فارسی
  • العربية
  • Deutsch
  • Français
  • Italiano
GitHub

1.8 kB gzipped0 dependenciesTypeScript

The sidebar that keeps up with your header.

Two-way sticky positioning for sidebars taller than the screen. It follows headers that slide away, collapse or resize, and works with plain JS, React and Vue.

Get started

Vanilla JSReactVue

TrailheadSearch gearCart (2)
CampHikeClimbSale

Category

Brand

Season

Weight

Rating

Apply filters
Ridgeline 2P tent$389
Alder 45 L pack$210
Cinder –5° bag$265
Pocket stove kit$64
Northpass trail runner$148
Lumen 400 headlamp$52
Kestrel 1P bivy$174
Merino base layer$89
Folding trekking poles$118
Lowland camp chair$72
Insulated mug, 350 ml$28
Repair kit$19
Free returns within 60 days
Stuck under the header
Stuck to the bottom

The orange column is a filter sidebar, taller than the window. Its top sits just under the header.

Why not just position: sticky

Where plain sticky falls short

out of reach

Tall sidebars get cut off

Plain sticky pins the top of a long sidebar, so its last items stay out of reach until the page ends.

→ Pins the bottom on the way down and the top on the way up.

empty gap

Hiding headers leave a gap

A fixed top value fits one header state. When the header slides away, the sidebar stays where it was.

→ Follows the header's bottom edge every frame while it moves.

covered

Growing headers cover it

Banners, search bars and compact modes change the header height while the page is open.

→ Re-measures on resize, class and style changes.

Setup

One function call, one CSS rule

Point it at the sidebar and the header. Put both columns in a grid that doesn't stretch the sidebar. Scroll handling, header tracking and cleanup are done for you.

The browser's own position: sticky moves the sidebar on every frame. The library only changes the sticky rules when the scroll direction flips or the header moves.

How it works →

Vanilla
import { stickySidebar } from 'dynamic-sticky-sidebar';

stickySidebar('#sidebar', {
    header: '.site-header',
    media: '(min-width: 768px)',
});
React
import { StickySidebar } from 'dynamic-sticky-sidebar/react';

<StickySidebar header=".site-header" media="(min-width: 768px)">
    <Filters />
</StickySidebar>
Vue
<script setup>
import { vStickySidebar } from 'dynamic-sticky-sidebar/vue';
const sticky = { header: '.site-header', media: '(min-width: 768px)' };
</script>

<template>
    <aside v-sticky-sidebar="sticky"><Filters /></aside>
</template>
.layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    align-items: start; /* keep the sidebar at its own height */
}

Docs

Read next

LayoutGrid, flex or block, and the overflow trap.Dynamic headersMultiple rows, animations and stores.API referenceEvery option and instance method.Live demoToggle a tall sidebar and an auto-hiding nav.
MIT Licensed