Bootstrap 5 Scroll To Top
A Scroll To Top button is a fixed-position UI element that appears after the user scrolls down a certain distance, allowing them to return to the top of the page with a single click. In Bootstrap 5, it is typically built using fixed positioning utilities, visibility toggling via JavaScript, and smooth scroll behaviour. It is most useful on long-form pages where repeated scrolling would otherwise frustrate users.
Primary Class
.scroll-to-topCommon Use Cases
- →Long editorial or blog pages with 2000+ words where readers finish an article and want to navigate the header menu without manual scrolling
- →E-commerce category pages with 40+ product listings loaded via pagination or infinite scroll, where the filter sidebar is anchored at the top
- →Documentation or knowledge base pages with multiple sections and a sticky table of contents that users need to revisit frequently
- →Single-page marketing sites with multiple full-height sections where anchor navigation is the primary wayfinding mechanism
Variants & Classes
| Variant | Description |
|---|---|
| Scroll To Top Default | Standard scroll to top with Bootstrap's default styling. |
| Scroll To Top Responsive | Responsive variant that adapts to different screen sizes. |
Code Example
<!-- Scroll To Top Button -->
<button
id="scrollToTopBtn"
class="btn btn-primary rounded-circle position-fixed bottom-0 end-0 m-4 d-none"
style="width: 48px; height: 48px; z-index: 1050;"
aria-label="Scroll to top"
onclick="window.scrollTo({ top: 0, behavior: 'smooth' })"
>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 16 16" aria-hidden="true">
<path fill-rule="evenodd" d="M8 15a.5.5 0 0 0 .5-.5V2.707l3.146 3.147a.5.5 0 0 0 .708-.708l-4-4a.5.5 0 0 0-.708 0l-4 4a.5.5 0 0 0 .708.708L7.5 2.707V14.5a.5.5 0 0 0 .5.5z"/>
</svg>
</button>
<!-- Scroll Visibility Script -->
<script>
const scrollBtn = document.getElementById('scrollToTopBtn');
window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
scrollBtn.classList.remove('d-none');
scrollBtn.classList.add('d-flex', 'align-items-center', 'justify-content-center');
} else {
scrollBtn.classList.add('d-none');
scrollBtn.classList.remove('d-flex', 'align-items-center', 'justify-content-center');
}
});
</script>Live Examples
Basic Scroll To Top
Canvas Framework Variants
The Canvas template extends Bootstrap 5 with 1,658+ component variants. Generate any of these using Canvas Builder:
- ✓Canvas Builder generated scroll to top with custom colours
- ✓Scroll To Top with interactive states
- ✓Responsive scroll to top for all screen sizes
Best Practices
Use a scroll threshold of 300px, not 0
Triggering the button immediately on any scroll creates visual noise above the fold. A 300px threshold ensures the button only appears once the user has genuinely moved away from the top of the page.
Avoid overlapping fixed footer elements on mobile
If your layout includes a fixed bottom navigation bar, increase the bottom offset using a custom utility or inline style — for example, set bottom to 72px — so the button does not sit on top of critical navigation controls on small screens.
Add a CSS transition for smooth visibility changes
Instead of a hard show/hide toggle, apply opacity and transition properties in CSS so the button fades in and out, which reduces the jarring effect of it appearing suddenly mid-scroll.
Always include an aria-label
Icon-only buttons have no visible text, so screen readers will announce nothing meaningful without an aria-label. Set aria-label="Scroll to top" to ensure accessibility compliance with WCAG 2.1 Success Criterion 1.1.1.