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.

JS Scroll Progress
Effect page →
View source code
HTML
<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>
CSS
.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
  }
}
JavaScript
(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.

JS Animation Scroll

Theme Toggle

Sun / moon theme switch: flips the whole page, follows the device setting and remembers the choice.

JS Toggle Transition

Countdown Timer

Flip-card countdown to a real end date; stops at zero.

JS Animation Timer

Copy to Clipboard

Copy button with animated confirmation and visual feedback.

JS Click Feedback

Share Buttons

Share buttons for X, Facebook, LinkedIn, WhatsApp and link copy (or the phone's share sheet), with labels on hover and keyboard focus.

JS Hover Social

Rating Stars

1 to 5 star rating by click, touch or keyboard, with animated fill.

JS Interactive Rating

Like / Heart Button

Heart button: beat and a burst of little hearts on every like, announced counter.

JS Click Particles

Bookmark Toggle

Bookmark icon with elastic save animation.

JS Toggle Animation

Notification Badge

Notification badge on a bell: breathes, bounces on each arrival, shakes then disappears when cleared.

JS Animation Badge

Adaptive Card

Layout switches from column to row based on container width.

@container CSS

Adaptive Card

Card that switches from vertical to horizontal layout via @container query when the container exceeds 280px.

CSS @container Layout
Effect page →
View source code
HTML
<div class="fx-0745-root">
  <div class="adc-card">
    <div class="adc-img"></div>
    <div class="adc-body">
      <h4>Adaptive Card</h4>
      <p>Layout switches from column to row based on container width.</p>
      <div class="adc-tags">
        <span class="adc-tag">@container</span>
        <span class="adc-tag">CSS</span>
      </div>
    </div>
  </div>
</div>
CSS
.fx-0745-root,
.fx-0745-root *,
.fx-0745-root *::before,
.fx-0745-root *::after {
  box-sizing: border-box
}

.fx-0745-root {
  container: adc-card / inline-size;
  display: block;
  width: 100%;
  background: #0a0a0f;
  border-radius: 14px;
  color: #fff;
  font-family: sans-serif;
}

.fx-0745-root .adc-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 14px;
  transition: all 0.3s ease;
}

.fx-0745-root .adc-img {
  width: 100%;
  height: 80px;
  border-radius: 10px;
  background: linear-gradient(135deg, #8b5cf6, #ec4899);
  background-size: cover;
  background-position: center;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.fx-0745-root .adc-body {
  min-width: 0;
}

.fx-0745-root .adc-body h4 {
  font-size: 0.85rem;
  font-weight: 700;
  color: #fff;
  margin: 0 0 4px;
}

.fx-0745-root .adc-body p {
  font-size: 0.72rem;
  color: rgba(255, 255, 255, 0.6);
  margin: 0 0 8px;
  line-height: 1.5;
}

.fx-0745-root .adc-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.fx-0745-root .adc-tag {
  padding: 3px 8px;
  border-radius: 6px;
  background: rgba(139, 92, 246, 0.15);
  font-size: 0.6rem;
  font-weight: 600;
  color: #a78bfa;
}

@container adc-card (min-width: 280px) {
  .fx-0745-root .adc-card {
    flex-direction: row;
    align-items: center;
  }
  .fx-0745-root .adc-img {
    width: 100px;
    height: 100px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .fx-0745-root .adc-card,
  .fx-0745-root .adc-img {
    transition: none;
  }
}
JavaScript
/* Pure CSS — no JavaScript required */

Responsive Nav

Icon tab bar that shows its labels once its container reaches 280 px.

CSS @container Nav

Fluid Typography

Text sizes adapting in 3 tiers via @container at 260px and 320px. Button included.

CSS @container Typography

Dashboard Widget

Analytics widget with adaptive stats grid (1→2→4 columns) and chart appearing at 300px.

CSS @container Dashboard

Product Card

Product card in three shapes: compact, with its description from 220 px, then a row with a “Details” button from 300 px.

CSS @container E-commerce

Stats Panel

Statistics panel switching from 1 to 2 columns with trend indicators that appear.

CSS @container Stats

Media Card

Video card that switches to horizontal mode with extended description when the container exceeds 280px.

CSS @container Media

Sidebar Toggle

Icon sidebar: the chevron expands it, its labels appear and the content beside it rearranges.

CSS JS @container Sidebar

Form Adapt

Form whose first and last name fields sit side by side from 280 px.

CSS @container Form

Grid Morph

Grid going from 1 to 2 then 3 columns; at the top tier the first cell spans 2×2.

CSS @container Grid

Container Query Cards

The card changes shape with its container width: compact, with text and button from 280 px, row from 400 px.

CSS @container Layout

Shape Hero

The banner clip-path goes from a slant to a curve, then to cut corners, based on container width.

CSS clip-path @container

Adaptive Navigation

The navigation changes shape: menu button, inline links, then sidebar depending on the container's width.

CSS JS @container Navigation

Fluid Grid Morph

Tiles go from 1 to 2 then 3 columns based on their container's width and glide to their new place (CSS transition).

CSS JS Grid @container

Typography Adapts

Typography adapts its size, weight and spacing based on container dimensions via @container.

CSS @container Typography

Breakpoint Morphing Cards

Card elements (image, text, buttons) physically reorganize at container breakpoints.

CSS @container Morph

Responsive Chart

The chart goes from a simple line to a bar chart, then to a labelled bar chart (months and values) depending on the space available.

CSS JS @container Data Viz

Sidebar Collapse

The sidebar switches from icons only to a full navigation with labels based on container width.

CSS @container Sidebar

Responsive Pricing Table

Plans go from a vertical stack to three columns side by side; features appear from 280 px and the cards glide to their new place.

CSS JS @container Pricing

Dynamic Aspect Ratio

The media container changes aspect ratio (1:1, 4:3, 16:9, 21:9) based on width via @container.

CSS @container Aspect Ratio

Gradient Border Spin

Spinning conic-gradient border using @property to animate the angle. The viral spinning border effect.

CSS @property Gradient
Effect page →
View source code
HTML
<div class="fx-0765-root">
  <div class="gsp-box">
    <a class="gsp-link" href="#gradient-spin">Gradient Spin</a>
  </div>
</div>
CSS
.fx-0765-root,
.fx-0765-root *,
.fx-0765-root *::before,
.fx-0765-root *::after {
  box-sizing: border-box
}

.fx-0765-root {
  --gsp-w: 200px;          /* box width */
  --gsp-h: 120px;          /* box height */
  --gsp-radius: 12px;      /* box corner radius (the border follows) */
  --gsp-width: 3px;        /* border thickness */
  --gsp-colors: #ff0080, #ff8c00, #40e0d0, #7b2ff7, #ff0080;
  --gsp-speed: 3s;         /* one turn at rest */
  --gsp-speed-hover: 1s;   /* one turn on hover, focus, tap */
  --gsp-halo: 4px;         /* halo blur on hover */
  --gsp-fill: #0a0a0f;     /* box fill */
  --gsp-ring: #fff;        /* keyboard focus ring */
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  position: relative;
  font-family: sans-serif;
}

@property --gsp-angle {
  syntax: '<angle>';
  initial-value: 0deg;
  inherits: false;
}

.fx-0765-root .gsp-box {
  width: var(--gsp-w);
  max-width: 100%;
  height: var(--gsp-h);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  font-weight: 600;
  color: white;
  border-radius: var(--gsp-radius);
  position: relative;
  font-family: inherit;
  user-select: none;
  background: var(--gsp-fill);
  z-index: 1;
}

.fx-0765-root .gsp-box::before {
  content: '';
  position: absolute;
  inset: calc(var(--gsp-width) * -1);
  border-radius: calc(var(--gsp-radius) + var(--gsp-width));
  background: conic-gradient(from var(--gsp-angle, 0deg), var(--gsp-colors));
  z-index: -2;
  animation: gspSpin var(--gsp-speed) linear infinite;
}

.fx-0765-root .gsp-box::after {
  content: '';
  position: absolute;
  inset: var(--gsp-width);
  background: var(--gsp-fill);
  border-radius: max(0px, calc(var(--gsp-radius) - var(--gsp-width)));
  z-index: -1;
}

.fx-0765-root .gsp-box:hover::before,
.fx-0765-root .gsp-box:focus-within::before {
  animation-duration: var(--gsp-speed-hover);
  filter: blur(var(--gsp-halo));
}

/* Stretched link: the whole box is clickable, and keyboard focus
   or a tap on this link triggers the same state as hover. */
.fx-0765-root .gsp-link {
  color: inherit;
  text-decoration: none;
  outline: none;
}

.fx-0765-root .gsp-link::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--gsp-radius);
}

.fx-0765-root .gsp-link:focus-visible::after {
  outline: 2px solid var(--gsp-ring);
  outline-offset: calc(var(--gsp-width) + 4px);
}

@keyframes gspSpin {
  to {
    --gsp-angle: 360deg;
  }
}

@media (prefers-reduced-motion: reduce) {
  .fx-0765-root .gsp-box::before,
  .fx-0765-root .gsp-box:hover::before,
  .fx-0765-root .gsp-box:focus-within::before {
    animation: none;
  }
}
JavaScript
/* Pure CSS — no JavaScript required */

Dashed Border March

Dashed border with dashes scrolling continuously via animated stroke-dashoffset.

SVG Animation Dash

Neon Border Pulse

Neon border that pulses with alternating intensity glow via animated box-shadow.

CSS Neon Glow

Border Draw-On

The border draws from a corner and traces the full perimeter with 4 strokes animated one after another.

CSS Hover Sequential

Double Line Border

Two concentric animated borders rotating in opposite directions with distinct colors.

CSS @property Multi-layer

Lightning Border

Electric border: an SVG feTurbulence filter whose lightning changes shape, below the flashing threshold.

SVG Filter Electric

Gradient Border Morph

Gradient border that fluidly morphs to a new color set on hover.

CSS Hover Gradient @property

Corner Accent Borders

Only corners are bordered, with colored accents that elongate on hover.

CSS Hover Corners

Border Ripple

On click, a light pulse travels along the border perimeter with propagating waves.

JS Click Ripple

Dotted Constellation Border

Dots along the border that link into a constellation on hover, focus or touch.

SVG JS Interactive

CSS Anchor Tooltip Pro

Tooltips tied to their button with CSS Anchor Positioning: they flip at the window edge and close with Escape. Automatic fallback in older browsers.

Anchor PositioningModern CSSPremium

Hypno Cascade

Crown of 24 rainbow petals breathing in a wave, 100% CSS (sibling-index() with --i fallback, OKLCH hues).

💡 Perfect for: waiting screens, AI generation states, decorative heroes, processing indicators.

CSS sibling-index Zero-JS Premium

Ruled Grid

Native gap rules in the grid gutters (Chrome 149+), drawn fallback elsewhere; counters animate into view.

💡 Perfect for: dashboards, stat grids, editorial tables, pricing pages.

CSS JS Gap Decorations Dashboard Premium

Flash Sale Countdown

Flip-clock countdown with a draining urgency bar, designed for flash sales. Duration and labels are customizable; connects to a real end date.

💡 Perfect for: flash sale, Black Friday, product launch, limited offer, promo banner, urgency pop-up.

Vanilla JS Flip-clock Urgency Premium

Frequently Asked Questions

How to enable smooth scroll in CSS? +

Add html { scroll-behavior: smooth } to your CSS. All anchor links (#section) will animate scrolling automatically. For finer control, use element.scrollIntoView({ behavior: 'smooth' }) in JavaScript.

How to copy text to clipboard in JavaScript? +

Use navigator.clipboard.writeText(text). It's the modern, async and secure API. Only works over HTTPS. As fallback, create a temporary textarea, select its content, and execute document.execCommand('copy').

How to create a dark mode toggle? +

Add a class (theme-light) on body that inverts colors via CSS custom properties. Save the preference in localStorage. On load, read localStorage and apply the class before rendering to avoid flash.

How to detect if an element is visible on screen? +

Use IntersectionObserver. Create an observer with the desired threshold (0.1 = 10% visible, 1 = 100%). Observe the element. The callback is called when visibility changes. More performant than a scroll listener.

How to debounce a function in JavaScript? +

Create a wrapper function that cancels the previous timer and creates a new one: function debounce(fn, delay) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), delay); }; }. Use for search, resize, scroll.

Similar Categories