PRACTICAL WEB TIP
READING 100 — Back to Top
A compact numerical scroll position and progress bar.
Design Preview
See the design above before trying the scroll interaction.
Live Demo
Press Try scrolling inside the frame, or scroll inside it. The button appears at the lower right and returns you to the top.
Source Code
Copy the HTML, CSS or JavaScript you need for your project.
<button type="button" class="tb-btt-button tb-btt-2031 is-hidden" aria-label="Back to the top of the page" title="Back to Top"><span class="art a31"><span class="a31-numbers"><span class="a31-percent">00</span><small>%</small></span><span class="a31-line"><i class="a31-fill"></i></span><span class="a31-bottom">↑ TOP</span></span></button>
/* TB-2031 — Back to Top button. */
.tb-btt-2031{--selectedtone:#466879;color:#466879;position:fixed;right:max(18px,env(safe-area-inset-right));bottom:max(18px,env(safe-area-inset-bottom));z-index:1000;border:0;background:transparent;padding:5px;display:flex;align-items:center;justify-content:center;min-width:48px;min-height:48px;cursor:pointer;font-family:Arial,"Noto Sans JP",sans-serif;line-height:normal;box-sizing:border-box;-webkit-tap-highlight-color:transparent;transition:opacity .24s ease, transform .35s ease,visibility .24s ease}
.tb-btt-2031:is(:focus-visible){outline:2px solid currentColor;outline-offset:4px}
.tb-btt-2031.is-hidden{opacity:0;transform:translateY(10px);visibility:hidden;pointer-events:none}
.tb-btt-2031 *{box-sizing:border-box}
.tb-btt-2031 .art{position:relative;display:flex;align-items:center;justify-content:center;flex-shrink:0}
.tb-btt-2031 .art{color:var(--selectedtone)}
.tb-btt-2031 .a31{width:83px;height:84px;padding:8px;border:1px solid #aec0c7;background:#eff5f7;border-radius:9px;flex-direction:column;gap:7px;color:#3d687c!important}
.tb-btt-2031 .a31-numbers{font:700 31px/1 Arial,sans-serif;letter-spacing:-.06em;display:flex;align-items:baseline}
.tb-btt-2031 .a31-numbers small{font:600 13px Arial,sans-serif;letter-spacing:0}
.tb-btt-2031 .a31-line{width:63px;height:4px;background:#c7dbe0;border-radius:3px;overflow:hidden}
.tb-btt-2031 .a31-fill{display:block;height:4px;width:100%;transform:scaleX(.72);transform-origin:left;background:#4d7c90;transition:transform .15s linear}
.tb-btt-2031 .a31-bottom{font:700 9px Arial,sans-serif;letter-spacing:1px;white-space:nowrap}
.tb-btt-2031:is(:hover,:focus-visible) .a31-fill{filter:brightness(1.3)}
.tb-btt-2031:is(:hover,:focus-visible) .a31-numbers{transform:translateY(-2px);transition:transform .45s}
@media(prefers-reduced-motion:reduce){.tb-btt-2031,.tb-btt-2031 *, .tb-btt-2031 *::before,.tb-btt-2031 *::after{animation-duration:0.01ms!important;animation-iteration-count:1!important;transition-duration:0.01ms!important}}
/* TB-2031 | Back to Top | No external libraries required. */
(() => {
'use strict';
const button = document.querySelector('.tb-btt-2031');
if (!button) return;
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
let scheduled = false;
function update() {
scheduled = false;
const visible = window.scrollY > 160;
button.classList.toggle('is-hidden', !visible);
button.tabIndex = visible ? 0 : -1;
button.setAttribute('aria-hidden', String(!visible));
const maxScroll = Math.max(1, document.documentElement.scrollHeight - window.innerHeight);
const progress = Math.max(0, Math.min(1, window.scrollY / maxScroll));
const number = button.querySelector('.a31-percent');
const fill = button.querySelector('.a31-fill');
if (number) number.textContent = String(Math.floor(progress * 100));
if (fill) fill.style.width = (progress * 100).toFixed(2) + '%';
}
function requestUpdate() {
if (scheduled) return;
scheduled = true;
requestAnimationFrame(update);
}
button.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: reduceMotion.matches ? 'auto' : 'smooth' });
});
window.addEventListener('scroll', requestUpdate, { passive: true });
window.addEventListener('resize', requestUpdate, { passive: true });
update();
})();
How to Use
Insert the HTML button near the end of body, load the CSS, and include JavaScript using defer. Test scrolling, returning to the top, and positioning on mobile.
Use Case
Long articles and reading tools
Technical Notes
Keep controls usable on touch devices. Test keyboard focus and reduced-motion preferences.
Mobile:Works with tap and keyboard. Check the lower-right offset against other fixed controls.
Browser:Uses standard HTML, CSS and JavaScript. Install all three together; no external library is required.
Reduced Motion:Reduces transitions and smooth scrolling when reduced motion is requested.