CSS Back to Top Button
A floating round button that smoothly scrolls the page back to the top.
HTML
<a class="to-top" href="#top" aria-label="Back to top">
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>
</a>
CSS
/* Smooth scrolling for anchor links (respects reduced motion) */
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
.to-top {
position: fixed;
right: 24px;
bottom: 24px;
display: grid;
place-items: center;
width: 48px;
height: 48px;
border-radius: 50%;
background: #0d9488;
box-shadow: 0 8px 20px -6px rgba(13, 148, 136, 0.6);
color: #fff;
transition: transform 0.2s ease, background 0.2s ease;
}
.to-top:hover {
background: #0f766e;
transform: translateY(-3px);
}
About this CSS Back to Top Button
This CSS Back to Top Button is 24 lines of pure CSS (0.5 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and CSS Grid. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
Customize it
The colors in this snippet. Search and replace them to match your brand:
#0d9488#fff#0f766e
How to use this back to top button
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.to-topclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.