Back to Top Button Generator
Generate customizable back-to-top buttons with smooth scroll animations
Preview
Generated Code
HTML
<button id="back-to-top" class="back-to-top" aria-label="Back to top">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 19V5M5 12l7-7 7 7" />
</svg>
</button>CSS
.back-to-top {
position: fixed;
bottom: 24px;
right: 24px;
width: 48px;
height: 48px;
background: #6366f1;
color: #ffffff;
border: none;
border-radius: 50%;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
transition: all 0.3s ease;
opacity: 0;
visibility: hidden;
}
.back-to-top.visible {
opacity: 1;
visibility: visible;
}
.back-to-top:hover {
background: #4f46e5;
transform: translateY(-4px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.25);
}
.back-to-top svg {
width: 24px;
height: 24px;
}
JavaScript
// Back to Top Button JavaScript
const backToTopButton = document.getElementById('back-to-top');
// Show/hide button based on scroll position
window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
backToTopButton.classList.add('visible');
} else {
backToTopButton.classList.remove('visible');
}
});
// Scroll to top on click
backToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
React Component
import { useState, useEffect } from 'react';
const BackToTopButton = () => {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const toggleVisibility = () => {
if (window.scrollY > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
window.addEventListener('scroll', toggleVisibility);
return () => window.removeEventListener('scroll', toggleVisibility);
}, []);
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
};
const buttonStyle = {
position: 'fixed',
bottom: 24,
right: 24,
width: 48,
height: 48,
background: '#6366f1',
color: '#ffffff',
border: 'none',
borderRadius: '50%',
boxShadow: '0 4px 15px rgba(0, 0, 0, 0.2)',
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
zIndex: 1000,
transition: 'all 0.3s ease',
opacity: isVisible ? 1 : 0,
visibility: isVisible ? 'visible' : 'hidden',
};
if (!isVisible) return null;
return (
<button
onClick={scrollToTop}
style={buttonStyle}
aria-label="Back to top"
>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" style={{ width: 24, height: 24 }}>
<path d="M12 19V5M5 12l7-7 7 7" />
</svg>
</button>
);
};
export default BackToTopButton;