Utilities & Responsive
Utilities, container queries, responsive shapes and animated borders.
Updated
Which small CSS and JavaScript utilities does a modern website need?
Utilities are small, reusable pieces for everyday needs: a scroll progress bar, a back-to-top button, a theme toggle, copy to clipboard, share and rating buttons, countdowns. They sit next to modern responsive techniques on this page: container queries, fluid shapes and animated gradient borders built with @property.
How to choose, performance and accessibility
- Add a Scroll Progress Bar to long articles, Back to Top on long pages, a Theme Toggle that remembers the choice and follows
prefers-color-scheme, and Copy to Clipboard next to code or promo codes. - Use the Clipboard API rather than the deprecated
execCommand, give icon-only buttons an accessible name, and debounce scroll and resize handlers. - Scroll Progress Bar, Adaptive Card and Gradient Border Spin are free with their full code; the other utilities come with Premium.
Scroll this block: the bar follows your reading.
Each paragraph you read brings the bar closer to the end of the block.
The bar only counts this block, not the whole page: that is the data-rpb-target setting.
Without it, the bar follows the page and sticks to the top of the window.
Screen readers announce the progress, from 0 to 100%.
You are almost there: one more paragraph.
End of the block. The bar is full.
Scroll Progress Bar
Thin gradient bar that fills as you read, for the page or a scrolling block.
Effect page →View source code
<div class="fx-0735-root" role="progressbar" aria-label="Reading progress" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<div class="rpb-bar"></div>
</div>.fx-0735-root,
.fx-0735-root *,
.fx-0735-root *::before,
.fx-0735-root *::after {
box-sizing: border-box
}
/* Default: bar fixed to the top of the window, it follows the page scroll. */
.fx-0735-root {
--rpb-height: 4px;
--rpb-track: transparent;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
height: var(--rpb-height);
background: var(--rpb-track);
pointer-events: none;
}
/* With data-rpb-target: put the root as the first child of the scrolling container,
it sticks to the top of that container without pushing its content. */
.fx-0735-root[data-rpb-target] {
position: sticky;
margin-bottom: calc(-1 * var(--rpb-height));
}
.rpb-bar {
height: 100%;
width: 100%;
transform: scaleX(0);
transform-origin: left center;
background: linear-gradient(90deg, #6366f1, #8b5cf6, #d946ef);
transition: transform 0.1s ease-out;
}
@media (prefers-reduced-motion: reduce) {
.rpb-bar {
transition: none
}
}(function () {
// One instance per root. Without data-rpb-target: the page. With it: the given container (CSS selector).
function run() {
document.querySelectorAll('.fx-0735-root').forEach(function (root) {
if (root.fx0735) return;
var bar = root.querySelector('.rpb-bar');
var sel = root.getAttribute('data-rpb-target');
var box = sel ? document.querySelector(sel) : null;
if (!bar || (sel && !box)) return;
var queued = false;
function update() {
queued = false;
var el = box || document.scrollingElement || document.documentElement;
var max = el.scrollHeight - el.clientHeight;
var p = max > 0 ? Math.min(1, Math.max(0, el.scrollTop / max)) : 0;
bar.style.transform = 'scaleX(' + p + ')';
root.setAttribute('aria-valuenow', Math.round(p * 100));
}
function schedule() {
if (!queued) { queued = true; requestAnimationFrame(update); }
}
(box || window).addEventListener('scroll', schedule, { passive: true });
window.addEventListener('resize', schedule);
// The content changes height (images, loaded text): recompute.
if ('ResizeObserver' in window) {
var ro = new ResizeObserver(schedule);
ro.observe(box || document.body);
if (box) Array.prototype.forEach.call(box.children, function (c) { ro.observe(c); });
}
root.fx0735 = { update: update };
update();
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run); else run();
})();Back to Top Button
Button that appears after 400 px of scrolling (adjustable) and smoothly brings you back to the top.
Effect page →Theme Toggle
Sun / moon theme switch: flips the whole page, follows the device setting and remembers the choice.
Effect page →Like / Heart Button
Heart button: beat and a burst of little hearts on every like, announced counter.
Effect page →Notification Badge
Notification badge on a bell: breathes, bounces on each arrival, shakes then disappears when cleared.
Effect page →