Key Takeaways
A sticky back to top button enhances user experience. Placing this button in the lower right-hand corner as a fixed element aids navigation, improving user engagement.
Consider design guidelines before implementing a back to top button. Questions on placement, size, and responsiveness ensure brand consistency and user accessibility.
Test the necessity of a back to top button for your site. Analyze user behavior using tools like Google Analytics or Hotjar to make data-driven decisions.
Implementing a back to top button involves HTML, CSS, and JavaScript. Understanding these languages is crucial for adding this feature to your WordPress site.
Weāve all been there. Youāve found a fantastic online guide, scrolled all the way to the bottom, and then think, āWow, Iād love to see what else this site has to offer!ā
But then you have to scroll.
ALL.
THE.
WAY.
TO.
THE.
TOP.
And then you thinkā¦. āHmmm, never mind.ā And close the page.
What is a sticky back to top button?
Also known as a scroll-to-top button or go-to-top image, the sticky back to top button is a helpful navigation element that helps users get back to the top of the web page theyāre viewing. A common UI pattern is to place this button in the lower right-hand corner of the screen, making it āstickyā via a fixed position so itās always accessible as the user scrolls down the page.
Things to consider before adding a back to top button
Like any popular design trend, we encourage you to take a step back before implementing it on your site to ask yourself: If Iām going to build this, what back top button guidelines do I need to follow?
Here are a few questions to answer before you build your scroll-to-top button:
- Where will it be placed?
- How big (or small) should it be?
- What design patterns should you follow so it stays on brand? (Think colors, fonts, icons, etc.)
- Is it at risk of covering important site content, such as information in a sidebar?
- What happens on mobile devices? Will it be responsive?
- Do you actually need it?*
*Note: You could make the argument that users today are conditioned to scroll down (and back up!) on a page, which would eliminate the āneedā for a back to top button. Our advice: Test it! Add a Google Analytics event on click or use a heat map tool like Hotjar to see how your site visitors engage with the page.
The best ābest practiceā to follow is one based on data from your own site and users!
How to add a sticky back to top button to your WordPress site
In this tutorial, weāll show you how to add a back to top button.
Pro-tip: While this tutorial isnāt too advanced, we still recommend trying it out on a staging site or in local environment, so thereās absolutely no risk to your live site. If you need to set one up quick, check out Local, a free local WordPress app thatās incredibly easy to use.
For this sticky back-to-top button tutorial, weāll use three languages:
- HTML for the markup to create the button
- CSS to style the button and have it match your brand
- JavaScript to make it work and define the behaviors of the button
Back to top button HTML
In the HTML, youāll find the following lines:
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003eu0026lt;a class=u0022top-link hideu0022 href=u0022u0022 id=u0022js-topu0022u0026gt;rn u0026lt;svg xmlns=u0022http://www.w3.org/2000/svgu0022 viewBox=u00220 0 12 6u0022u0026gt;u0026lt;path d=u0022M12 6H0l6-6zu0022/u0026gt;u0026lt;/svgu0026gt;rn u0026lt;span class=u0022screen-reader-textu0022u0026gt;Back to topu0026lt;/spanu0026gt;rnu0026lt;/au0026gt;u003c/preu003e TThis is going to be your sticky back to top button! You can see that weāve added a CSS class called .top-link, and are using some simple JavaScript to make it work. Weāre also using an in-line SVG for the button.
Besides an SVG, you could also use an image file or font icon to create the button. We prefer the SVG method, however, because:
- It wonāt get pixelated at different screen sizes, like a raster image would.
- SVGs are universally supported across browsers. (Yay, user experience!)
- Itās easy to style with CSS, so you can change everything about it really easily.
- It only takes one line of code, making it lightweight and better for site performance.
The last really important piece youāll find in the HTML is this line:
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003eu0026lt;span class=u0022screen-reader-textu0022u0026gt;Back to topu0026lt;/spanu0026gt;u003c/preu003e This is critical for users operating with screen readers and will improve the accessibility of your WordPress site. (Think of it like an alt tag for an image, but for your scroll-to-top button!)
CSS for back to top button
Now letās talk more about that CSS class, .top-link. Weāre using this to actually style up the button, and itās where youāll define qualities such as how big itāll be, how much padding should be around it, the color, etc.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003e.top-link {rn transition: all .25s ease-in-out;rn position: fixed;rn bottom: 0;rn right: 0;rn display: inline-flex;rn rn cursor: pointer;rn align-items: center;rn justify-content: center;rn margin: 0 3em 3em 0;rn border-radius: 50%;rn padding: .25em;rn width: 80px;rn height: 80px;rn background-color: #F8F8F8;u003c/preu003e Note: Weāre using Sass (a stylesheet language), to write our CSS a little bit faster. Learn more about this preprocessor here.
A couple important pieces from this snippet: transition: all .25s ease-in-out; controls how āfastā your button will appear or disappear on the screen, and position: fixed; is what makes the button āstickā to the location you want it.
Next, youāll see rules for .show and .hide. Weāll use JavaScript to switch between these classes in order to tell the browser when the button should (or shouldnāt) appear on the page.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003e .show {rn visibility: visible;rn opacity: 1;rn }rn rn .hide {rn visibility: hidden;rn opacity: 0;rn }u003c/preu003e Before we go into the JavaScript, there are just a few more lines weāll add to the CSS.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003esvg {rn fill: #000;rn width: 24px;rn height: 12px;rn}rnu0026amp;:hover {rn background-color: #E8E8E8;rn tsvg {rnt fill: #000000;rnt}rn}u003c/preu003e These classes will stylize the SVG image for the arrow itself and tell the browser how to display the button when a user hovers over it.
Finally, weāll add some CSS to hide the text that says āback to topā for screen readers.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003e// Text meant only for screen readers.rn.screen-reader-text {rntposition: absolute;rntclip-path: inset(50%);rntmargin: -1px;rntborder: 0;rntpadding: 0;rntwidth: 1px;rntheight: 1px;rntoverflow: hidden;rntword-wrap: normal !important;rntclip: rect(1px, 1px, 1px, 1px);rntu0026amp;:focus {rnttdisplay: block;rntttop: 5px;rnttleft: 5px;rnttz-index: 100000; // Above WP toolbarrnttclip-path: none;rnttbackground-color: #eee;rnttpadding: 15px 23px 14px;rnttwidth: auto;rnttheight: auto;rntttext-decoration: none;rnttline-height: normal;rnttcolor: #444;rnttfont-size: 1em;rnttclip: auto !important;rnt}rn}u003c/preu003e JavaScript for back to top button
Now on to the JavaScript! Weāre going to do this without loading jQuery, which will help keep your code lightweight and quick to load.
The first variable will help the browser find the button.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003e// Set a variable for our button element.rnconst scrollToTopButton = document.getElementById('js-top');u003c/preu003e Next, weāll create a function that shows the scroll-to-top button if the user scrolls beyond the height of the initial window.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003econst scrollFunc = () =u0026gt; {rn // Get the current scroll valuern let y = window.scrollY;rn rn // If the scroll value is greater than the window height, let's add a class to the scroll-to-top button to show it!rn if (y u0026gt; 0) {rn scrollToTopButton.className = u0022top-link showu0022;rn } else {rn scrollToTopButton.className = u0022top-link hideu0022;rn }rn};u003c/preu003e Weāll also add an event listener, which will watch as the user scrolls (so we know where theyāre at on the page).
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003ewindow.addEventListener(u0022scrollu0022, scrollFunc);u003c/preu003e Almost done! Next, we need to define the function that makes the button actually take the user back to the top of the page. To do that, weāll create a variable for the number of pixels we are from the top of the page. If that number is greater than 0, the function will set it back to 0, taking us to the top!
Weāll also add a little animation here, so the jump isnāt too sudden.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003econst scrollToTop = () =u0026gt; {rn // Let's set a variable for the number of pixels we are from the top of the document.rn const c = document.documentElement.scrollTop || document.body.scrollTop;rn rn // If that number is greater than 0, we'll scroll back to 0, or the top of the document.rn // We'll also animate that scroll with requestAnimationFrame:rn // https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFramern if (c u0026gt; 0) {rn window.requestAnimationFrame(scrollToTop);rn // ScrollTo takes an x and a y coordinate.rn // Increase the '10' value to get a smoother/slower scroll!rn window.scrollTo(0, c - c / 10);rn }rn};u003c/preu003e Last but not least, we just need to tell the page to run that function when someone clicks our sticky back-to-top button.
u003cpre class=u0022wp-block-syntaxhighlighter-codeu0022u003e// When the button is clicked, run our ScrolltoTop function above!rnscrollToTopButton.onclick = function(e) {rn e.preventDefault();rn scrollToTop();rn}u003c/preu003e Thatās it! Youāll now have a fully functioning and customizable sticky back to top button on your WordPress site.
If you want even better performance from your site, turn to WP Engine and our hosting for WordPress sites to push your digital presence to the next level!