Free code generator

Before/After Comparison Slider Generator

This is a code generator: design a draggable before/after comparison slider component here, then copy a standalone HTML, CSS and JS snippet to embed on your own website with your own two images.

After
Before
Standalone HTML + CSS + JS

A real, working drag mechanic

This preview is fully interactive so you can confirm the mechanic before you copy the code.

Pointer-based drag

Uses the Pointer Events API, clamped between 0% and 100%, for mouse, touch and pen alike.

Three handle styles

Line-only, circle with arrows, or circle with a chevron — themed with one color variable.

Standalone snippet

Copy self-contained HTML, CSS and JS with clear comments on where your images go.

How to generate your slider

  1. Label your panelsSet the left and right labels, like "Before" and "After" or "2019" and "2024".
  2. Style the handleChoose a handle style, color, divider width, starting position and corner radius.
  3. Copy and swap imagesCopy the snippet, then replace the two placeholder divs with your own img tags.

About before/after comparison sliders

A comparison slider is a small interactive widget that reveals one image as the user drags a handle over another, commonly used for photo edits, redesigns, product renders or time-lapse comparisons. Building one from scratch means wiring up pointer or touch events, clamping the position between 0% and 100%, and updating a clip-path or width so the reveal tracks the handle smoothly. This generator produces exactly that: a small, dependency-free component using CSS custom properties for color and sizing, so it's simple to theme and safe to drop into an existing page without style conflicts. The placeholder panels here use gradients purely so you can see the mechanic clearly — in your own project you'll replace them with two real images of the same size.

Clip-path reveal

The generated JS updates a clip-path inset on the top panel as the handle moves, which is smoother than resizing width in most browsers.

Pointer Events

One event model (pointerdown/pointermove/pointerup) covers mouse, touch and pen input without separate touch-specific code.

Clamped 0-100%

The handle position is always clamped, so fast drags or edge clicks can't push it outside the widget.

CSS custom properties

Handle color and divider width are exposed as CSS variables in the snippet, making retheming a one-line change.

Comparison slider generator FAQ

Does this generate an image comparison, or a code snippet?

This tool generates a reusable, standalone HTML, CSS and JS code snippet for a before/after comparison slider component, meant to be pasted into your own website with your own two images.

How do I use my own images with the generated code?

Swap the two placeholder panel divs' background styles for your own img tags, exactly as noted in the code comment included in the export.

Does the slider work on touch devices?

Yes. The generated JS uses the Pointer Events API, which covers mouse, touch and pen input with a single set of event listeners.

Can I theme the handle color and divider width?

Yes. Handle color, divider line width, handle style, starting position and corner radius are all configurable and exposed as CSS custom properties in the output.

Try other visual tools

Continue building media and hover components with these free tools.