Sticky reader header with progress in HTML, CSS and vanilla JS
A copy-paste Sticky reader header with progress in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-sticky-header-progress" data-dcl-scheme="light">
<div class="dcl-sticky-header-progress-scroller" data-scroller role="region" aria-label="Scrollable print guide">
<header class="dcl-sticky-header-progress-bar" data-panel><h2>Print guide</h2><div data-row><label for="dcl-sticky-header-progress-meter">Reading progress</label>
<progress id="dcl-sticky-header-progress-meter" data-wide max="100" value="0"></progress></div></header>
<article class="dcl-sticky-header-progress-body"><h3 id="dcl-sticky-header-progress-prepare" tabindex="-1">Prepare the artwork</h3><p>Check margins and fold lines on a printed proof.</p><p>Leave an extra centimetre along the trimmed edge.</p><p>Send the approved proof to the production desk.</p><h3 id="dcl-sticky-header-progress-print" tabindex="-1">Make the impression</h3><p>Apply a thin, even layer of ink with the roller.</p><p>Pull one impression on scrap paper to check coverage.</p><p>Allow the finished copies to dry flat before stacking.</p><h3 id="dcl-sticky-header-progress-finish" tabindex="-1">Finish the edition</h3><p>Check the fold of each copy before trimming.</p><p>Number the approved copies in pencil.</p><p>Store one untrimmed copy with the workshop notes.</p><p><a href="#dcl-sticky-header-progress-prepare">Back to the first step</a></p></article></div>
<script>
(() => {
const root = document.currentScript.parentElement;
const instance = [...document.getElementsByClassName("dcl-sticky-header-progress")].indexOf(root);
if (instance > 0) {
for (const node of root.querySelectorAll("[id], [for], [aria-labelledby], [aria-controls], [aria-describedby], a[href], input[name]")) {
for (const key of ["id", "for", "aria-labelledby", "aria-controls", "aria-describedby", "name"]) {
if (node.hasAttribute(key)) node.setAttribute(key, node.getAttribute(key).split(" ").map(value => value + "--" + instance).join(" "));
}
const href = node.getAttribute("href");
if (href && href.startsWith("#")) node.setAttribute("href", href + "--" + instance);
}
}
const scheme = matchMedia("(prefers-color-scheme: dark)");
const applyScheme = () => { root.dataset.dclScheme = scheme.matches ? "dark" : "light"; };
scheme.addEventListener("change", applyScheme);
applyScheme();
const scroller = root.querySelector("[data-scroller]");
const meter = root.querySelector("progress");
const update = () => {
const height = scroller.scrollHeight - scroller.clientHeight;
meter.value = height > 0 ? scroller.scrollTop / height * 100 : 100;
};
scroller.tabIndex = 0;
scroller.addEventListener("scroll", update, {passive: true});
new ResizeObserver(update).observe(scroller.querySelector("article"));
update();
root.querySelectorAll('a[href^="#"]').forEach(link => {
link.addEventListener("click", event => {
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return;
const target = document.getElementById(link.getAttribute("href").slice(1));
if (!target || !root.contains(target)) return;
event.preventDefault();
target.focus({preventScroll: true});
target.scrollIntoView({block: "nearest"});
});
});
})();
</script>
</section>
<style>
.dcl-sticky-header-progress { box-sizing: border-box; max-inline-size: 42rem; padding: clamp(.75rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-sticky-header-progress *, .dcl-sticky-header-progress *::before, .dcl-sticky-header-progress *::after { box-sizing: border-box; }
.dcl-sticky-header-progress h2, .dcl-sticky-header-progress h3, .dcl-sticky-header-progress p, .dcl-sticky-header-progress ul, .dcl-sticky-header-progress dl, .dcl-sticky-header-progress figure { margin: 0; }
.dcl-sticky-header-progress h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-sticky-header-progress h3 { font-size: 1rem; }
.dcl-sticky-header-progress > * + * { margin-block-start: .75rem; }
.dcl-sticky-header-progress button, .dcl-sticky-header-progress summary, .dcl-sticky-header-progress input, .dcl-sticky-header-progress select, .dcl-sticky-header-progress textarea { font: inherit; }
.dcl-sticky-header-progress button, .dcl-sticky-header-progress summary { padding: .45rem .7rem; min-block-size: 2.75rem; border: 1px solid; border-radius: .375rem; cursor: pointer; text-align: start; }
.dcl-sticky-header-progress select, .dcl-sticky-header-progress input[type="text"], .dcl-sticky-header-progress textarea { max-inline-size: 100%; padding: .45rem; border: 1px solid; border-radius: .375rem; }
.dcl-sticky-header-progress button:active { transform: translateY(1px); }
.dcl-sticky-header-progress button[disabled], .dcl-sticky-header-progress button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; transform: none; }
.dcl-sticky-header-progress button[aria-pressed="true"] { border-style: double; border-width: 3px; }
.dcl-sticky-header-progress details > summary { inline-size: fit-content; }
.dcl-sticky-header-progress [data-panel], .dcl-sticky-header-progress dialog { border: 1px solid; border-radius: .625rem; padding: .8rem; }
.dcl-sticky-header-progress dialog { max-inline-size: min(calc(100% - 2rem), 28rem); max-block-size: calc(100% - 2rem); }
.dcl-sticky-header-progress dialog > * + * { margin-block-start: .75rem; }
.dcl-sticky-header-progress [data-row] { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; min-inline-size: 0; }
.dcl-sticky-header-progress [data-stack] { display: grid; gap: .6rem; min-inline-size: 0; }
.dcl-sticky-header-progress [data-list] { list-style: none; padding: 0; display: grid; gap: .55rem; }
.dcl-sticky-header-progress [data-wide] { flex: 1 1 11rem; min-inline-size: 0; }
.dcl-sticky-header-progress .dcl-sticky-header-progress-status { min-block-size: 1.5em; font-weight: 600; }
.dcl-sticky-header-progress .dcl-sticky-header-progress-glyph { display: inline-grid; place-items: center; font-size: 1.2em; line-height: 1; }
.dcl-sticky-header-progress [hidden] { display: none; }
.dcl-sticky-header-progress { color: #11292f; background-color: #ffffff; color-scheme: light; border-color: #697f84; }
.dcl-sticky-header-progress a { color: #005c62; }
.dcl-sticky-header-progress button, .dcl-sticky-header-progress summary, .dcl-sticky-header-progress input, .dcl-sticky-header-progress select, .dcl-sticky-header-progress textarea { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-sticky-header-progress .dcl-sticky-header-progress-primary, .dcl-sticky-header-progress button[aria-pressed="true"] { color: #ffffff; background-color: #00535a; }
.dcl-sticky-header-progress button:hover, .dcl-sticky-header-progress summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-sticky-header-progress button[disabled], .dcl-sticky-header-progress button[aria-disabled="true"] { color: #405a60; background-color: #ffffff; }
.dcl-sticky-header-progress [data-panel], .dcl-sticky-header-progress dialog { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-sticky-header-progress :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; border-color: #839fa5; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] button, .dcl-sticky-header-progress[data-dcl-scheme="dark"] summary, .dcl-sticky-header-progress[data-dcl-scheme="dark"] input, .dcl-sticky-header-progress[data-dcl-scheme="dark"] select, .dcl-sticky-header-progress[data-dcl-scheme="dark"] textarea { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] .dcl-sticky-header-progress-primary, .dcl-sticky-header-progress[data-dcl-scheme="dark"] button[aria-pressed="true"] { color: #11292f; background-color: #8fe1da; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] button:hover, .dcl-sticky-header-progress[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] button[disabled], .dcl-sticky-header-progress[data-dcl-scheme="dark"] button[aria-disabled="true"] { color: #b0c9ce; background-color: #142f36; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] [data-panel], .dcl-sticky-header-progress[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-sticky-header-progress[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-sticky-header-progress .dcl-sticky-header-progress-scroller { block-size: 16rem; overflow: auto; scroll-padding-block-start: 4.75rem; } .dcl-sticky-header-progress .dcl-sticky-header-progress-body { padding: .8rem; } .dcl-sticky-header-progress .dcl-sticky-header-progress-body > * + * { margin-block-start: .65rem; } .dcl-sticky-header-progress .dcl-sticky-header-progress-bar { position: sticky; inset-block-start: 0; z-index: 1; border-radius: 0; }
</style>
When to use
Keep reading progress available in a long self-contained guide without adding permanent page chrome.
Avoid
Do not show decorative progress that is not tied to a real scroll position.
Accessibility
Name the progress element without putting every scroll update into a live region; keep focusable content visible below the sticky inset. Light/dark body text: 15.19:1 and 13.07:1, respectively.
Was this useful? Send feedback.