Component Library

← Back to gallery

Scroll-revealed return action in HTML, CSS and vanilla JS

A copy-paste Scroll-revealed return action in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Navigation · Tags: back-to-top, scroll, anchor, long-page

Live preview

Source (copy-ready)

<section class="dcl-back-to-top-after-sentinel" data-dcl-scheme="light">
<h2>Scroll-revealed return action</h2>
<div data-reader><div data-scroll><h3 id="dcl-back-to-top-after-sentinel-start" tabindex="-1">Volunteer handbook</h3><article><p>Before opening the studio, check the equipment board and note any items that need a repair.</p><h3>After the shift</h3><p>Return keys and log unfinished tasks for the next team.</p></article></div><div data-returnbar><a href="#dcl-back-to-top-after-sentinel-start" data-return hidden>Return to handbook start</a></div></div>
<script>
(() => {
const root = document.currentScript.closest('.dcl-back-to-top-after-sentinel');
const instance = Array.from(document.getElementsByClassName('dcl-back-to-top-after-sentinel')).indexOf(root);
if (instance) {
  const suffix = '--' + instance;
  const attributes = ['id','aria-labelledby','aria-describedby','aria-controls','for','name','popovertarget'];
  for (const element of root.querySelectorAll('*')) {
    for (const attr of attributes) {
      if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr).split(/\s+/).map(value => value + suffix).join(' '));
    }
    const href = element.getAttribute('href');
    if (href && href.startsWith('#')) element.setAttribute('href', href + suffix);
  }
}
const media = matchMedia('(prefers-color-scheme: dark)');
const theme = () => { root.dataset.dclScheme = media.matches ? 'dark' : 'light'; };
media.addEventListener('change', theme);
theme();
root.addEventListener('click', event => {
  const anchor = event.target.closest('a[href]');
  if (!anchor || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
  const href = anchor.getAttribute('href');
  if (!href.startsWith('#')) return;
  const target = root.querySelector(href);
  if (!target) return;
  event.preventDefault();
  target.focus({preventScroll: true});
  target.scrollIntoView({block: 'nearest'});
});
const pane = root.querySelector('[data-scroll]');
const title = pane.querySelector('h3');
const control = root.querySelector('[data-return]');
const update = () => { control.hidden = pane.scrollTop < title.offsetHeight + 12 && document.activeElement !== control; };
pane.addEventListener('scroll', update);
control.addEventListener('blur', () => requestAnimationFrame(update));
})();
</script>
</section>
<style>
.dcl-back-to-top-after-sentinel { box-sizing: border-box; max-width: 58rem; padding: clamp(.75rem, 3vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui,sans-serif; overflow-wrap: anywhere; }
.dcl-back-to-top-after-sentinel *, .dcl-back-to-top-after-sentinel *::before, .dcl-back-to-top-after-sentinel *::after { box-sizing: border-box; }
.dcl-back-to-top-after-sentinel h2, .dcl-back-to-top-after-sentinel h3, .dcl-back-to-top-after-sentinel p, .dcl-back-to-top-after-sentinel ul, .dcl-back-to-top-after-sentinel ol, .dcl-back-to-top-after-sentinel figure { margin: 0; }
.dcl-back-to-top-after-sentinel h2 { font-size: 1.25rem; margin-bottom: .875rem; }
.dcl-back-to-top-after-sentinel h3 { font-size: 1rem; }
.dcl-back-to-top-after-sentinel li + li, .dcl-back-to-top-after-sentinel p + p { margin-top: .375rem; }
.dcl-back-to-top-after-sentinel ul, .dcl-back-to-top-after-sentinel ol { padding-inline-start: 1.35rem; }
.dcl-back-to-top-after-sentinel a { text-underline-offset: .18em; }
.dcl-back-to-top-after-sentinel a:hover { text-decoration-thickness: .18em; }
.dcl-back-to-top-after-sentinel :focus-visible { outline-style: solid; outline-width: 3px; outline-offset: 2px; }
.dcl-back-to-top-after-sentinel button, .dcl-back-to-top-after-sentinel summary, .dcl-back-to-top-after-sentinel input, .dcl-back-to-top-after-sentinel select { font: inherit; max-width: 100%; }
.dcl-back-to-top-after-sentinel button, .dcl-back-to-top-after-sentinel input, .dcl-back-to-top-after-sentinel select { padding: .35rem .65rem; border: 1px solid; border-radius: .375rem; }
.dcl-back-to-top-after-sentinel button, .dcl-back-to-top-after-sentinel summary, .dcl-back-to-top-after-sentinel select { cursor: pointer; }
.dcl-back-to-top-after-sentinel button:active, .dcl-back-to-top-after-sentinel summary:active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-back-to-top-after-sentinel button:disabled, .dcl-back-to-top-after-sentinel button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; }
.dcl-back-to-top-after-sentinel [hidden] { display: none !important; }
.dcl-back-to-top-after-sentinel [data-line], .dcl-back-to-top-after-sentinel [data-actions] { display: flex; gap: .5rem .75rem; align-items: center; flex-wrap: wrap; }
.dcl-back-to-top-after-sentinel [data-grid] { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,9rem),1fr)); gap: .75rem; }
.dcl-back-to-top-after-sentinel [data-stack], .dcl-back-to-top-after-sentinel [data-targets] { display: grid; gap: .75rem; }
.dcl-back-to-top-after-sentinel [data-targets] { border-top: 1px solid; padding-top: .75rem; margin-top: .75rem; }
.dcl-back-to-top-after-sentinel [data-card], .dcl-back-to-top-after-sentinel [data-status], .dcl-back-to-top-after-sentinel [data-panel] { padding: .75rem; border: 1px solid; border-radius: .5rem; }
.dcl-back-to-top-after-sentinel [data-status] { margin-top: .75rem; }
.dcl-back-to-top-after-sentinel [data-status]:empty { border-width: 0; padding: 0; margin: 0; }
.dcl-back-to-top-after-sentinel [aria-current] { font-weight: 750; }
.dcl-back-to-top-after-sentinel dialog, .dcl-back-to-top-after-sentinel [popover] { max-height: calc(100dvh - 2rem); max-width: calc(100% - 2rem); width: min(36rem,100%); border: 1px solid; border-radius: .75rem; padding: 1rem; overflow: auto; }
.dcl-back-to-top-after-sentinel dialog h3, .dcl-back-to-top-after-sentinel [popover] h3 { margin-bottom: .5rem; }
.dcl-back-to-top-after-sentinel dialog [data-actions], .dcl-back-to-top-after-sentinel [popover] [data-actions] { margin-top: .75rem; }
.dcl-back-to-top-after-sentinel svg { display: block; width: 100%; height: auto; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] a { color: #005c62; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] button, .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] input, .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] select, .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] summary, .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] [data-card], .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] [data-status], .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] [data-panel] { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] button:hover, .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] dialog, .dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] [popover] { color: #11292f; background-color: #ffffff; border-color: #697f84; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="light"] :focus-visible { outline-color: #005c62; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] button, .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] input, .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] select, .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] summary, .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] [data-card], .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] [data-status], .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] [data-panel] { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] button:hover, .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] dialog, .dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] [popover] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; }
.dcl-back-to-top-after-sentinel[data-dcl-scheme="dark"] :focus-visible { outline-color: #8fe1da; }
.dcl-back-to-top-after-sentinel [data-scroll] { border: 1px solid; max-height: 14rem; overflow: auto; padding: .75rem; }
.dcl-back-to-top-after-sentinel [data-scroll] article { min-height: 25rem; }
.dcl-back-to-top-after-sentinel [data-returnbar] { min-height: 2.4rem; padding-top: .4rem; }
</style>

When to use

Toolbar return action appears only after the pane's first heading scrolls out of view. Replace the example content and wire the controls or destination IDs to your own screen.

Avoid

Don't use as the only navigation.

Accessibility

Keyboard focus is visibly outlined. Never hide the link while it has focus. The main text contrast is 15.2:1/13.1:1 in light/dark mode, respectively.

Was this useful? Send feedback.