CSS components

Before and After Caption Generator

Show a transformation with a draggable divider: labels on each side of the image, a caption underneath and a handle that works with mouse, touch and keyboard.

Live preview

Runs entirely in your browser. Nothing is uploaded to any server.

Why use a labeled comparison slider

Shows the change

Visitors drag to see exactly what changed between two images.

Context included

Labels and a caption explain what each side shows.

Accessible

A real range input works with keyboard and screen readers.

How to build a before and after slider

  1. Add both imagesUse two images with the same size and framing for the best result.
  2. Write labels and captionName each side and describe the change in the caption.
  3. Copy the codeCopy the figure, the CSS and the six-line script.

How the comparison works

The after image sits in normal flow and sets the height. The before image is stacked on top and trimmed with clip-path: inset(), driven by a single CSS custom property called --pos. Moving the divider only changes that property, so it is smooth even on slow phones.

An invisible range input covers the whole image. It gives you mouse dragging, touch dragging and arrow-key control for free, and screen readers announce it as a slider. touch-action: pan-y still lets people scroll the page vertically on phones.

Same size images

Export both images at the same dimensions so they line up exactly.

Figure and caption

figure and figcaption tie the caption to the comparison.

Focus outline

Keyboard users see an outline on the handle when it has focus.

Before and after slider FAQ

Why should both images be the same size?

The after image sets the frame and the before image is stretched to cover it, so different aspect ratios would crop or misalign the before image.

Can I control the slider with the keyboard?

Yes. Tab to the image and use the arrow keys, Home or End, because the control is a native range input.

Does it work on phones?

Yes. Drag anywhere on the image horizontally; vertical swipes still scroll the page.

Can I have several comparisons on one page?

Yes. The script sets up every element with the class ba-frame.

Can I make the divider vertical instead?

This generator makes a side-by-side comparison. For a top and bottom split, change the clip-path inset to the top and bottom values and move the handle with top instead of left.

Try other CSS component tools

More free tools from My Panda Toolbox.