
You have just finished reading a long article on your phone. You want to get back to the menu at the top — but the page is thousands of pixels tall, and swiping back up takes forever. That small frustration is exactly what a back-to-top button solves: one tap, and the page glides back to the beginning.
A back-to-top button is a small floating button, usually a circle or rounded square with an upward arrow, that appears in a corner of the screen once a visitor has scrolled down a page. Tapping it scrolls the page smoothly back to the top. It is a tiny feature, but on long pages — blog posts, product listings, documentation, and landing pages — it makes a site noticeably easier to use. This guide shows you how to add one with plain HTML, CSS, and a short JavaScript snippet, how to style it so it fits your site, and how to avoid the mistakes that make these buttons annoying.
What a Back-to-Top Button Is — and When Your Site Needs One
A back-to-top button does one job: it takes the visitor back to the top of the page. It stays hidden while the visitor is near the top, then fades in once they have scrolled a good distance down. The button usually sits fixed in the bottom-right corner, so it is always within reach, and tapping it triggers a smooth scroll rather than an instant jump.
Your site benefits from one when your pages are long. Blog posts, help-centre articles, product category pages with dozens of items, and long landing pages all qualify. If a page is longer than about three screen heights, most visitors will never scroll all the way back up by hand — they will either use the browser's own controls, close the tab, or simply never return to the top navigation. A button removes that friction.
Short pages do not need one. If the whole page fits on one or two screens, a floating button is just clutter. The same applies if your site already has a sticky navigation bar that follows the visitor down the page — they can already reach the menu from anywhere. The button is a complement to good navigation, not a replacement for it.
A few signs that your site needs one:
- Long articles or guides. Readers who finish a 3,000-word post are at the very bottom of the page, far from your menu, your other posts, and your contact links.
- Deep scrolling on mobile. Phone screens show less at once, so mobile visitors scroll further and more often — they feel the missing shortcut most.
- A footer worth reaching. If your footer holds important links — services, contact details, legal pages — visitors need a quick way back up, and a quick way back down is only half the journey.
- One-page sites. A long single page with several sections is exactly where a back-to-top button earns its place.
Plan the Button Before You Build It
A back-to-top button takes ten minutes to build, but five minutes of planning saves you from rebuilding it later. Decide four things first: where it sits, how big it is, when it appears, and what it looks like.
Position. Bottom-right is the standard, and visitors expect it there. Use bottom-left only when something else already lives in the bottom-right — a chat widget, for example. Keep the button clear of cookie banners, sticky bars, and floating contact buttons, or it will overlap them on smaller screens.
Size. Make the tap target at least 44 pixels across. That is the size a fingertip can hit comfortably, and anything smaller turns into a frustrating tap-the-wrong-thing exercise on a phone. On desktop the same size looks balanced; a 44 to 56 pixel circle is the usual range.
When it appears. Show the button only after the visitor has scrolled past roughly 300 to 500 pixels — far enough that it never appears on short pages, and early enough that it is there when it is needed. Fade it in rather than popping it into view; a sudden appearance draws the eye away from the content.
Design. Match your site. Use your brand colour or a neutral dark circle with a white arrow — the upward arrow is understood everywhere, in every language. Keep the button slightly transparent until it is hovered over, so it does not compete with your content while it waits.
Step 1: Add the Button to Your HTML
The button itself is one line of HTML. Place it near the end of your page, just before the closing </body> tag, so it loads after the main content:
<button id="back-to-top" aria-label="Back to top" title="Back to top">↑</button>A few details in that line matter. Use a <button> element, not a link, because the button performs an action on the page rather than taking the visitor somewhere else. The id is how the CSS and JavaScript will find it. The aria-label tells screen readers what the button does, because an arrow character on its own means nothing to someone who cannot see it. The title shows a small tooltip — “Back to top” — when a desktop visitor hovers over it.
The arrow character (↑) works everywhere and needs no image file. If you want a crisper arrow, replace the character with a small inline SVG of an upward arrow — the result looks sharper on high-resolution screens and scales cleanly at any size.
Step 2: Style It with CSS
The CSS positions the button, shapes it, and controls how it appears and disappears. Add this to your stylesheet:
#back-to-top {
position: fixed;
bottom: 24px;
right: 24px;
width: 48px;
height: 48px;
border: none;
border-radius: 50%;
background: #4f46e5;
color: #fff;
font-size: 1.4rem;
cursor: pointer;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
z-index: 100;
}
#back-to-top.visible {
opacity: 1;
visibility: visible;
}
#back-to-top:hover {
background: #4338ca;
}Here is what the important lines do. position: fixed pins the button to the corner of the browser window, so it stays put while the page scrolls underneath. bottom and right set the distance from the edges — 24 pixels is a comfortable gap. The border-radius: 50% turns the square button into a circle, and the box-shadow lifts it off the page so it reads as a floating control rather than part of the content.
The button starts invisible: opacity: 0 and visibility: hidden. When JavaScript adds the visible class, the transition fades it in over a third of a second. Using both opacity and visibility matters — opacity alone would leave an invisible button that visitors could still accidentally tap. The z-index: 100 keeps the button above your page content, but below full-screen overlays such as pop-ups and menus, so it never blocks anything important. Change the background colour to match your own site — the indigo here is only an example.
Step 3: Show and Hide It with JavaScript
The JavaScript has two jobs: watch how far the visitor has scrolled, and scroll back to the top when the button is clicked. The whole script is about ten lines:
const button = document.getElementById('back-to-top');
window.addEventListener('scroll', function () {
button.classList.toggle('visible', window.scrollY > 400);
});
button.addEventListener('click', function () {
window.scrollTo({ top: 0, behavior: 'smooth' });
});The scroll listener runs every time the page moves. It checks window.scrollY — the number of pixels the page has scrolled down — and adds the visible class once the visitor passes 400 pixels. Below that point the class is removed and the button fades out again. The click handler then scrolls the page to the very top with behavior: 'smooth', so the page glides up instead of jumping.
Keep the scroll listener light. It fires many times per second while a visitor scrolls, so it should do almost nothing — a single class toggle is ideal. Avoid putting heavy work, layout reads, or network calls inside it, or scrolling will feel sluggish on older phones.
If you would rather skip JavaScript entirely, there is a simpler version: an anchor link that points to the top of the page, combined with scroll-behavior: smooth on the html element. It works, but you lose the show-and-hide behaviour — the link would sit on the page permanently. For a floating button that appears only when it is useful, the small script above is the better route.
Smooth Scrolling — and Respecting Reduced-Motion Settings
Smooth scrolling is what makes the button feel good instead of jarring. An instant jump to the top disorients visitors — they lose their place and have to re-read the page to work out where they are. A smooth scroll, lasting well under a second, keeps the sense of where they came from.
Not everyone wants animation, though. Some visitors set their operating system to reduce motion because animations make them dizzy or unwell, and browsers pass that preference on to websites. Respect it by checking the setting before you animate:
button.addEventListener('click', function () {
const reduceMotion = window.matchMedia(
'(prefers-reduced-motion: reduce)'
).matches;
window.scrollTo({
top: 0,
behavior: reduceMotion ? 'auto' : 'smooth'
});
});With this version, visitors who asked for reduced motion get an instant jump — exactly what they prefer — while everyone else gets the smooth glide. It is a small addition that costs nothing and makes the feature work for everyone.
There is one more detail worth handling: keyboard users. After the page scrolls to the top, a visitor navigating by keyboard is still focused on the button at the bottom of the page, which is confusing. Move the focus to the top as well — for example, to your header or skip link — by giving that element a tabindex="-1" and calling focus() on it after the scroll. It is a one-line addition that makes the button genuinely usable for everyone.
Making It Work Well on Mobile
Mobile visitors scroll the most, so they benefit from a back-to-top button the most — but phones also have the least room for error. Four things deserve attention on small screens.
Thumb reach. The bottom-right corner is the easiest spot for right-handed users to reach with their thumb, which is why it is the standard position. If your audience is largely left-handed, bottom-left is a reasonable alternative — but do not overthink it: consistency with what visitors expect matters more than handedness.
Safe areas. Modern phones have a home indicator bar at the bottom of the screen. On iPhones, add env(safe-area-inset-bottom) to the button's bottom offset so it sits above that bar instead of overlapping it: bottom: calc(24px + env(safe-area-inset-bottom)).
Overlaps. Phones stack floating elements quickly — a chat widget, a cookie banner, a sticky add-to-cart bar, and your new button can all claim the same corner. Open your site on a real phone, scroll to the bottom, and look: if anything overlaps, move one of them. A button that covers your checkout button is worse than no button at all.
Size. The 44-pixel minimum matters most on mobile. Test the button with your own thumb, not just your mouse — a target that looks fine on a desktop monitor can feel tiny on a phone.
Adding a Back-to-Top Button in WordPress or a Website Builder
If your site runs on WordPress, check your theme first — many themes include a back-to-top option in the Customizer, under Appearance, where you can switch it on and choose its colour and position. If your theme does not offer it, a free plugin such as WPFront Scroll Top adds one with settings for position, colour, size, and when it appears. Install the plugin, adjust the settings, and the button appears on every page.
Website builders handle it differently. Wix has a built-in back-to-top button setting you can switch on from the site settings. Squarespace and similar builders usually need a short code snippet pasted into the code injection area — the same HTML, CSS, and JavaScript from this guide, added in one place, will work there too.
Whichever route you take, the checks are the same: the button should appear only after scrolling, it should not overlap your chat widget or cookie banner, and it should scroll smoothly back to the top. Test it on your phone as well as your desktop before you call it done.
Common Mistakes That Make Back-to-Top Buttons Annoying
Most back-to-top buttons fail in the same handful of ways. Check yours against this list before you publish.
- Always visible. A button that sits on the screen from the moment the page loads is clutter, not help. Hide it until the visitor has scrolled.
- Too big or too bright. A giant button in a loud colour competes with your content and your calls to action. It should be noticeable, not dominant.
- Overlapping other floating elements. The chat widget, the cookie banner, and the back-to-top button all want the same corner. Give each one its own space.
- Jumping instead of scrolling. An instant jump to the top feels broken. Smooth scrolling — or an instant jump only for visitors who asked for reduced motion — is the right behaviour.
- No label for screen readers. An arrow with no accessible name is invisible to assistive technology. The aria-label costs nothing and makes the button usable for everyone.
- Covering important content. If the button sits on top of a sticky bar, a form field, or a checkout button, it is actively hurting conversions. Test the bottom of every page type.
- Ignoring reduced motion. Some visitors find scrolling animations uncomfortable. Check the preference and skip the animation for them.
Conclusion
A back-to-top button is one of the smallest features you can add to a website, and one of the most appreciated. It is a single button, a few lines of CSS, and a ten-line script — and it turns a long, tiring scroll back to the top into a single tap. Add it to your long pages, keep it subtle, test it on a real phone, and respect visitors who prefer reduced motion. Your readers will not notice the button when it works well — they will only notice that getting around your site feels easy.
Frequently Asked Questions
Do I need JavaScript to add a back-to-top button?
A basic version works with just HTML and CSS — an anchor link to the top of the page plus smooth scrolling. But JavaScript is needed to show and hide the button based on how far the visitor has scrolled, which is what makes the button feel polished. The script in this guide is about ten lines.
Will a back-to-top button slow down my website?
No, in any meaningful way. It is one button, a few lines of CSS, and a tiny scroll listener that does almost no work. It adds almost nothing to the page weight and has no measurable effect on load time.
Where should the button appear on the page?
The bottom-right corner is the standard position, about 20 to 30 pixels from the edges. Use bottom-left only if something else — a chat widget, for example — already occupies the bottom-right.
Should the button show on mobile phones too?
Yes — mobile visitors scroll the longest pages and benefit from it the most. Keep the button at least 44 pixels across, keep it clear of the phone's home indicator bar and any chat widget, and test it on a real phone.
Can I add a back-to-top button in WordPress with a plugin?
Yes. Many themes include a back-to-top option in the Customizer, and free plugins such as WPFront Scroll Top add one with settings for position, colour, and when it appears — no theme editing required.
Does a back-to-top button help SEO?
Not directly — Google does not rank pages higher for having one. It helps indirectly: visitors who can move around easily stay longer and leave less frustrated, and a good user experience supports the engagement signals search engines notice.
