Sticky chapter markers in HTML, CSS and vanilla JS
A copy-paste Sticky chapter markers 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-chapter" data-dcl-scheme="light">
<nav aria-label="Print handbook chapters"><ul data-row><li><a href="#dcl-sticky-header-chapter-prepare" >Prepare the artwork</a></li><li><a href="#dcl-sticky-header-chapter-print" >Make the impression</a></li><li><a href="#dcl-sticky-header-chapter-finish" >Finish the edition</a></li></ul></nav>
<div class="dcl-sticky-header-chapter-scroller" data-scroller role="region" aria-label="Scrollable print handbook"><section><h3 class="dcl-sticky-header-chapter-bar" data-panel id="dcl-sticky-header-chapter-prepare" tabindex="-1">1. Prepare the artwork</h3><div class="dcl-sticky-header-chapter-body"><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></div></section><section><h3 class="dcl-sticky-header-chapter-bar" data-panel id="dcl-sticky-header-chapter-print" tabindex="-1">2. Make the impression</h3><div class="dcl-sticky-header-chapter-body"><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></div></section><section><h3 class="dcl-sticky-header-chapter-bar" data-panel id="dcl-sticky-header-chapter-finish" tabindex="-1">3. Finish the edition</h3><div class="dcl-sticky-header-chapter-body"><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></div></section></div>
<script>
(() => {
const root = document.currentScript.parentElement;
const instance = [...document.getElementsByClassName("dcl-sticky-header-chapter")].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();
root.querySelector("[data-scroller]").tabIndex = 0;
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-chapter { 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-chapter *, .dcl-sticky-header-chapter *::before, .dcl-sticky-header-chapter *::after { box-sizing: border-box; }
.dcl-sticky-header-chapter h2, .dcl-sticky-header-chapter h3, .dcl-sticky-header-chapter p, .dcl-sticky-header-chapter ul, .dcl-sticky-header-chapter dl, .dcl-sticky-header-chapter figure { margin: 0; }
.dcl-sticky-header-chapter h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-sticky-header-chapter h3 { font-size: 1rem; }
.dcl-sticky-header-chapter > * + * { margin-block-start: .75rem; }
.dcl-sticky-header-chapter button, .dcl-sticky-header-chapter summary, .dcl-sticky-header-chapter input, .dcl-sticky-header-chapter select, .dcl-sticky-header-chapter textarea { font: inherit; }
.dcl-sticky-header-chapter button, .dcl-sticky-header-chapter summary { padding: .45rem .7rem; min-block-size: 2.75rem; border: 1px solid; border-radius: .375rem; cursor: pointer; text-align: start; }
.dcl-sticky-header-chapter select, .dcl-sticky-header-chapter input[type="text"], .dcl-sticky-header-chapter textarea { max-inline-size: 100%; padding: .45rem; border: 1px solid; border-radius: .375rem; }
.dcl-sticky-header-chapter button:active { transform: translateY(1px); }
.dcl-sticky-header-chapter button[disabled], .dcl-sticky-header-chapter button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; transform: none; }
.dcl-sticky-header-chapter button[aria-pressed="true"] { border-style: double; border-width: 3px; }
.dcl-sticky-header-chapter details > summary { inline-size: fit-content; }
.dcl-sticky-header-chapter [data-panel], .dcl-sticky-header-chapter dialog { border: 1px solid; border-radius: .625rem; padding: .8rem; }
.dcl-sticky-header-chapter dialog { max-inline-size: min(calc(100% - 2rem), 28rem); max-block-size: calc(100% - 2rem); }
.dcl-sticky-header-chapter dialog > * + * { margin-block-start: .75rem; }
.dcl-sticky-header-chapter [data-row] { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; min-inline-size: 0; }
.dcl-sticky-header-chapter [data-stack] { display: grid; gap: .6rem; min-inline-size: 0; }
.dcl-sticky-header-chapter [data-list] { list-style: none; padding: 0; display: grid; gap: .55rem; }
.dcl-sticky-header-chapter [data-wide] { flex: 1 1 11rem; min-inline-size: 0; }
.dcl-sticky-header-chapter .dcl-sticky-header-chapter-status { min-block-size: 1.5em; font-weight: 600; }
.dcl-sticky-header-chapter .dcl-sticky-header-chapter-glyph { display: inline-grid; place-items: center; font-size: 1.2em; line-height: 1; }
.dcl-sticky-header-chapter [hidden] { display: none; }
.dcl-sticky-header-chapter { color: #11292f; background-color: #ffffff; color-scheme: light; border-color: #697f84; }
.dcl-sticky-header-chapter a { color: #005c62; }
.dcl-sticky-header-chapter button, .dcl-sticky-header-chapter summary, .dcl-sticky-header-chapter input, .dcl-sticky-header-chapter select, .dcl-sticky-header-chapter textarea { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-sticky-header-chapter .dcl-sticky-header-chapter-primary, .dcl-sticky-header-chapter button[aria-pressed="true"] { color: #ffffff; background-color: #00535a; }
.dcl-sticky-header-chapter button:hover, .dcl-sticky-header-chapter summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-sticky-header-chapter button[disabled], .dcl-sticky-header-chapter button[aria-disabled="true"] { color: #405a60; background-color: #ffffff; }
.dcl-sticky-header-chapter [data-panel], .dcl-sticky-header-chapter dialog { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-sticky-header-chapter :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; border-color: #839fa5; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] button, .dcl-sticky-header-chapter[data-dcl-scheme="dark"] summary, .dcl-sticky-header-chapter[data-dcl-scheme="dark"] input, .dcl-sticky-header-chapter[data-dcl-scheme="dark"] select, .dcl-sticky-header-chapter[data-dcl-scheme="dark"] textarea { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] .dcl-sticky-header-chapter-primary, .dcl-sticky-header-chapter[data-dcl-scheme="dark"] button[aria-pressed="true"] { color: #11292f; background-color: #8fe1da; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] button:hover, .dcl-sticky-header-chapter[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] button[disabled], .dcl-sticky-header-chapter[data-dcl-scheme="dark"] button[aria-disabled="true"] { color: #b0c9ce; background-color: #142f36; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] [data-panel], .dcl-sticky-header-chapter[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-sticky-header-chapter[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-sticky-header-chapter .dcl-sticky-header-chapter-scroller { block-size: 16rem; overflow: auto; scroll-padding-block-start: 4.75rem; } .dcl-sticky-header-chapter .dcl-sticky-header-chapter-body { padding: .8rem; } .dcl-sticky-header-chapter .dcl-sticky-header-chapter-body > * + * { margin-block-start: .65rem; } .dcl-sticky-header-chapter .dcl-sticky-header-chapter-bar { position: sticky; inset-block-start: 0; z-index: 1; border-radius: 0; } .dcl-sticky-header-chapter .dcl-sticky-header-chapter-scroller { scroll-padding-block-start: 0; } .dcl-sticky-header-chapter nav ul { list-style: none; padding: 0; } .dcl-sticky-header-chapter nav a { display: inline-block; padding-block: .3rem; } .dcl-sticky-header-chapter section + section { border-block-start: 1px solid; }
</style>
When to use
Make the current chapter recognizable while scrolling a long handbook.
Avoid
Do not use many nested sticky bands that obscure each other.
Accessibility
Use correct heading levels and scroll padding so fragment navigation and focused content are not hidden. Light/dark body text: 15.19:1 and 13.07:1, respectively.
Was this useful? Send feedback.