Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Add Smooth Scrolling to a “Back to Top” Button in Your Web Portal

Use native CSS for page-wide smooth scrolling, JavaScript for a button-only action, and the scrolling element’s own API for nested portal panels.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a normal page that scrolls in the browser viewport, add scroll-behavior: smooth to the html element. If animation should occur only when the button is clicked, call window.scrollTo({ top: 0, behavior: "smooth" }) in the button’s event handler. Apply the rule or API to the element that actually scrolls.

Option 1: Enable smooth scrolling with CSS

The shortest page-level implementation is:

html {
  scroll-behavior: smooth;
}

This controls scrolling initiated by navigation and CSSOM scrolling APIs on the root scrolling box; it does not animate scrolling performed directly by the user. See the MDN scroll-behavior reference.

As an Amazon Associate I earn from qualifying purchases.

Respect reduced-motion preferences

Some visitors ask their operating system or browser to reduce motion. Restrict the animation to users who have not made that request:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

Outside that media query, the browser uses its normal scrolling behavior.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Option 2: Animate only the Back to Top button

Use the Window scrolling API when the smooth effect belongs specifically to the button:

<button type="button" id="back-to-top">Back to top</button>

<script>
  const backToTopButton = document.querySelector("#back-to-top");

  backToTopButton.addEventListener("click", () => {
    window.scrollTo({
      top: 0,
      behavior: "smooth"
    });
  });
</script>

top: 0 targets the top of the document viewport. The MDN window.scrollTo() documentation defines behavior: "smooth" for an animated scroll and behavior: "auto" for the computed CSS behavior.

Combine the handler with reduced-motion support

If you use JavaScript, check the visitor’s preference before requesting animation:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const backToTopButton = document.querySelector("#back-to-top");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");

backToTopButton.addEventListener("click", () => {
  window.scrollTo({
    top: 0,
    behavior: reduceMotion.matches ? "auto" : "smooth"
  });
});

Make sure you target the real scrolling element

The viewport and an in-page portal panel are different scrolling boxes. A rule on an element that does not scroll cannot animate another element’s scrolling.

Document viewport

For ordinary page scrolling, put the CSS declaration on html. MDN notes that the root-level rule applies to the viewport; placing it on body does not propagate it to the viewport. MDN explains the root-scrolling behavior.

Nested overflow container

If your portal keeps content in a scrollable panel, give that panel the behavior:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
.portal-content {
  overflow-y: auto;
  scroll-behavior: smooth;
}

Then scroll the panel itself rather than the window:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const panel = document.querySelector(".portal-content");

panel.querySelector(".back-to-top").addEventListener("click", () => {
  panel.scrollTo({
    top: 0,
    behavior: "smooth"
  });
});

Choosing between CSS and JavaScript

Approach Best for Scope Important limitation
html { scroll-behavior: smooth; } A site-wide, declarative default Root scrolling triggered by navigation or CSSOM APIs The browser chooses the easing and duration
window.scrollTo({ top: 0, behavior: "smooth" }) A button-specific action Only the code path that calls it You must attach and maintain the event handler
Element scroll-behavior and element.scrollTo() Portals with an overflow panel The selected scrolling container Using window will not move the nested panel

What timing and compatibility to expect

The smooth value requests an animation, but CSS does not provide a duration property for it. Easing and duration are chosen by the user agent, so timing can vary between browsers and devices. User agents are also permitted to ignore scroll-behavior; provide a usable instant-scroll fallback rather than depending on the animation.

MDN lists the CSS property as widely available across browsers since March 2022. Check the current compatibility details on MDN when your portal has a strict browser-support requirement.

If code needs to wait for a promise from a scrolling operation, feature-detect that behavior: MDN notes that some browsers do not support promise-returning scroll operations even though smooth scrolling itself may work. See the API’s compatibility notes.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting checklist

  • Nothing moves: confirm the button’s click handler runs and that the page or panel has content above the current position.
  • The page jumps instantly: check whether the browser is honoring smooth behavior, whether reduced motion is enabled, or whether another script calls scrollTo with behavior: "auto".
  • body { scroll-behavior: smooth; } has no effect: move the declaration to html for viewport scrolling.
  • The wrong area scrolls: identify the element with overflow: auto or overflow: scroll and call that element’s scrollTo().
  • The button submits a form: keep type="button" on the control so its click does not submit the surrounding form.
  • Motion is uncomfortable: honor prefers-reduced-motion: reduce and allow an instant return to the top.

Recommended implementation

For most web portals, start with the reduced-motion-aware CSS rule:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

Add the explicit window.scrollTo handler when only the Back to Top control should animate, or use the equivalent element API when the portal scrolls inside a nested container. The SitePoint discussion that prompted this question is archived at SitePoint Community.

Quick Recap

SaleBestseller No. 1
HTML and CSS: Design and Build Websites
HTML and CSS: Design and Build Websites
HTML CSS Design and Build Web Sites; Comes with secure packaging; It can be a gift option
$14.94
SaleBestseller No. 3
SaleBestseller No. 4
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.