Bootstrap 5 Sticky Sidebar
A sticky sidebar is a layout pattern where a secondary column remains fixed in the viewport as the user scrolls through the main content area. Bootstrap 5 achieves this using the `position-sticky` utility combined with `top-*` offset classes, requiring no JavaScript. Use it when you have supplementary content — such as a table of contents, filters, or author bio — that users benefit from seeing throughout a long page.
Primary Class
.sticky-sidebarCommon Use Cases
- →Blog post pages where a table of contents sidebar tracks the reader's position and stays visible as they scroll through 2,000+ word articles
- →E-commerce category pages where product filter controls (price range, brand, size) need to remain accessible while the user browses a long product grid
- →Documentation sites where a section navigation menu sticks so developers can jump between API reference sections without scrolling back to the top
- →Dashboard layouts where a summary widget showing totals or KPIs stays pinned while the user reviews detailed data rows in the main content panel
Variants & Classes
| Variant | Description |
|---|---|
| Sticky Sidebar Default | Standard sticky sidebar with Bootstrap's default styling. |
| Sticky Sidebar Responsive | Responsive variant that adapts to different screen sizes. |
Code Example
<div class="container py-5">
<div class="row">
<!-- Main Content -->
<div class="col-lg-8">
<article>
<h1 class="mb-3">Understanding Async JavaScript</h1>
<p>JavaScript's event loop handles asynchronous operations through a call stack and callback queue...</p>
<h2 class="mt-5">Promises</h2>
<p>A Promise represents a value that may be available now, in the future, or never...</p>
<h2 class="mt-5">Async/Await</h2>
<p>The async/await syntax is syntactic sugar over Promises, making asynchronous code read like synchronous code...</p>
<h2 class="mt-5">Error Handling</h2>
<p>Use try/catch blocks inside async functions to handle rejected Promises cleanly...</p>
</article>
</div>
<!-- Sticky Sidebar -->
<div class="col-lg-4">
<div class="sticky-top" style="top: 80px;">
<div class="card border-0 shadow-sm">
<div class="card-body">
<h5 class="card-title fw-semibold mb-3">On This Page</h5>
<nav class="nav flex-column">
<a class="nav-link px-0 py-1 text-body" href="#promises">Promises</a>
<a class="nav-link px-0 py-1 text-body" href="#async-await">Async/Await</a>
<a class="nav-link px-0 py-1 text-body" href="#error-handling">Error Handling</a>
</nav>
</div>
</div>
<div class="card border-0 shadow-sm mt-3">
<div class="card-body">
<h6 class="fw-semibold">Written by</h6>
<div class="d-flex align-items-center gap-2 mt-2">
<img src="/assets/avatar-sarah.jpg" class="rounded-circle" width="40" height="40" alt="Sarah Chen">
<div>
<p class="mb-0 fw-medium">Sarah Chen</p>
<small class="text-muted">Senior JS Engineer</small>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>Live Examples
Basic Sticky Sidebar
Canvas Framework Variants
The Canvas template extends Bootstrap 5 with 1,658+ component variants. Generate any of these using Canvas Builder:
- ✓Canvas Builder generated sticky sidebar with custom colours
- ✓Sticky Sidebar with interactive states
- ✓Responsive sticky sidebar for all screen sizes
Best Practices
Set a precise top offset to clear fixed navbars
Bootstrap's `sticky-top` class sets `top: 0` by default, which causes the sidebar to slide under a fixed navbar. Override it with an inline style or a custom CSS variable — `style="top: 72px;"` — matching your navbar's exact height so the sticky element stops in the right position.
Constrain sidebar height to prevent overflow on short viewports
If your sidebar content is taller than the viewport, add `max-height: calc(100vh - 80px); overflow-y: auto;` to the sticky container so users can scroll within the sidebar independently without breaking the layout.
Only apply sticky behaviour on large screens
On mobile, a sticky sidebar competes for screen space with the main content. Wrap the `sticky-top` class conditionally using a custom breakpoint class, or apply `position: sticky` only inside a `@media (min-width: 992px)` rule so small-screen users see a normal stacked layout.
Avoid sticky sidebars inside overflow-hidden parents
`position: sticky` silently breaks when any ancestor element has `overflow: hidden` or `overflow: auto` set. If your sidebar refuses to stick, inspect parent elements in DevTools — removing the rogue overflow property almost always fixes it.