Components

Building an interactive before/after image comparison slider with zero dependencies

Side-by-side photo comparisons never convey transformation as persuasively as an interactive split slider. Pairing a simple HTML range input with CSS clipping creates a fluid, tactile wiping experience that converts hesitant prospective buyers.

Before / After Slider

How to Use the Before / After Slider (3 Simple Steps)

1

Configure the Reveal & Handle

Choose horizontal or vertical orientation, set the starting split position, pick handle and divider colours, and toggle the drag-hint arrow icon inside the handle.

2

Add Labels & Fine-Tune Motion

Enable Before/After text labels with custom copy, adjust corner radius for a rounded frame, and optionally add a CSS transition so the divider eases to its resting position.

3

Copy the CSS + HTML Snippet

Switch to the HTML tab and copy the self-contained page — replace the placeholder image paths with your real before/after photos and drop it into any project.

Features & CSS Best Practices

✓

Pointer Events API Drag

Uses the unified Pointer Events API (pointerdown / pointermove / pointerup) so the same drag code works for mouse, touch, and stylus inputs without any framework overhead.

✓

CSS Custom Property Architecture

The slider position is stored in a single CSS custom property (--ba-slider-pos). The clip-path, divider, and handle all read from it, making the JS a one-liner update and enabling CSS-side transitions.

✓

WCAG-Compliant Keyboard Support

The handle carries role="slider" with aria-valuemin/max/now and responds to arrow keys (2% step, 10% with Shift) so keyboard-only users can control the reveal without a mouse or touch screen.

✓

Reduced-Motion Safe

When a drag-transition duration is set, the generated CSS wraps the animation in @media (prefers-reduced-motion: reduce) { transition: none } so users who opt out of motion always get an instant reveal.

Frequently Asked Questions

How does the CSS clip-path technique work for the before/after effect?

The "Before" image is absolutely positioned and clipped with clip-path: inset(). As the slider moves, a CSS custom property updates the inset value, progressively revealing more or less of the Before image on top of the After image beneath it. No JavaScript animation loop is needed — a single style.setProperty() call per pointer event is enough.

Can I use this slider with real photographs instead of gradient backgrounds?

Yes. Replace the two gradient placeholder divs in the generated HTML with <img> elements pointing to your before and after photos. Keep a hidden spacer with the same dimensions so the container maintains the correct aspect ratio without JavaScript.

What is the difference between horizontal and vertical orientation?

Horizontal orientation reveals the Before image from left to right — ideal for side-by-side photo retouching comparisons. Vertical orientation reveals top to bottom, which works well for before/after UI screenshots, before/after fashion shots, or any composition where the subject change is best shown along the vertical axis.

Does this slider work on mobile touchscreens?

Yes. The Pointer Events API handles both touch and mouse events natively. The container also sets touch-action: pan-y (horizontal mode) or touch-action: pan-x (vertical mode) so the page can still scroll in the other axis while the slider is being dragged. No additional touch-event polyfills are required.

How does an interactive before-after comparison slider work in CSS?

A before-after slider layers two images exactly on top of each other. An HTML range input controls the clip-path: inset(0 0 0 calc(100% - var(--pos))) of the top image or container width, revealing the bottom image underneath as the handle moves.

Can a before/after image slider be made fully keyboard accessible?

Yes. By binding the slider handle to a native HTML <input type="range">, users can navigate and adjust comparison positions using Arrow keys, Home, and End keys, with full screen reader aria-valuenow announcements.

How do I prevent image distortion or resizing in a split-screen slider?

Set object-fit: cover; and identical width and height dimensions on both before and after images. This ensures both images share identical proportions and pixel alignment regardless of the reveal split position.

Understanding CSS Before/After Image Comparison Sliders

A before/after image comparison slider lets users drag a handle across two overlapping images to reveal one beneath the other. Historically these required heavyweight jQuery plugins, but modern CSS — specifically clip-path: inset() driven by CSS custom properties — achieves the same effect with a few dozen lines of code and zero dependencies.

The key insight is using a CSS custom property (like --ba-slider-pos) as the single source of truth for the divider position. The clipped image, the visible divider line, and the draggable handle all read from this one value. JavaScript only needs to call el.style.setProperty() once per pointer event — there is no layout recalculation loop, no requestAnimationFrame needed, and transitions animate automatically via CSS.

Accessibility is often overlooked in slider widgets. The generated code assigns role="slider", aria-valuemin, aria-valuemax, and aria-valuenow to the handle element, and wires up arrow-key listeners so keyboard-only users can step through the reveal in 2% increments (or 10% with Shift). This satisfies WCAG 2.1 success criterion 2.1.1 (Keyboard).

Core CSS — clip-path driven by a custom property css
.ba-slider {
  position: relative;
  overflow: hidden;
  user-select: none;
}
.ba-slider__before {
  position: absolute;
  inset: 0;
  clip-path: inset(0 calc(100% - var(--ba-slider-pos, 50%)) 0 0);
}
.ba-slider__handle {
  position: absolute;
  left: var(--ba-slider-pos, 50%);
  top: 50%;
  transform: translate(-50%, -50%);
  cursor: ew-resize;
}
Minimal Pointer Events drag handler javascript
const el = document.querySelector('.ba-slider');
let dragging = false;

function setPos(x) {
  const pct = (x / el.offsetWidth) * 100;
  el.style.setProperty('--ba-slider-pos', pct.toFixed(2) + '%');
}

el.addEventListener('pointerdown', e => {
  dragging = true;
  el.setPointerCapture(e.pointerId);
  setPos(e.offsetX);
});
el.addEventListener('pointermove', e => { if (dragging) setPos(e.offsetX); });
el.addEventListener('pointerup',   () => { dragging = false; });

Browser Compatibility & Standards Support

Verified across modern desktop and mobile browser engines according to W3C specifications.

FeatureChromeFirefoxSafariEdge
clip-path: inset()55+54+10.1+79+
CSS Custom Properties49+31+9.1+15+
Pointer Events API55+59+13+12+
@media (prefers-reduced-motion)74+63+10.1+79+

Architecture Best Practices & Production Tips

💡 Use aspect-ratio instead of a spacer image

If you know your image dimensions ahead of time, replace the hidden spacer with a CSS aspect-ratio rule on the container (e.g. aspect-ratio: 16 / 9) to avoid an extra HTTP request and keep the markup cleaner.

💡 Animate only on release, not while dragging

Setting a CSS transition on the clip-path while the user drags creates visible lag. Instead, add the transition class only on pointerup and remove it on pointerdown: this gives a smooth settle-to-rest animation after the drag without slowing real-time tracking.