How to use the before and after slider builder
- Add two images. Drop in the before and after versions. They should be the same size and framing, such as two exports of the same design or a photo before and after editing. A sample pair is loaded first.
- Try the slider. Drag across the preview, or click it and use the arrow keys. Choose a left–right or top–bottom split and where the handle starts.
- Style it. Turn the labels on or off and rename them, then set the handle colour and corner radius.
- Copy the code. Copy the HTML, CSS and short script, then change before.jpg and after.jpg to your image paths. Or download a standalone demo page with the images embedded.
How the slider works
Both images sit in the same grid cell, one on top of the other. The after image is trimmed with clip-path: inset(), controlled by a CSS custom property. An invisible, full-size range input sits on top: dragging it updates the property, so the browser does all the work and the script is only three lines long.
Built to be accessible
Because the control is a real <input type="range">, it works with a mouse, touch and the keyboard out of the box, shows a focus ring, and is announced by screen readers with a clear label. Both images have alt text, and the labels are hidden from assistive technology so they aren’t read twice.
Good uses
Before-and-after sliders are ideal for showing a website redesign, photo retouching, image compression quality, colour grading, renovation projects and dark versus light themes. Use images with identical dimensions and alignment, or the comparison will jump as you drag.
Frequently asked questions
Does it work on phones?
Yes. The range input responds to touch, and the slider scales with the width of its container.
Do I need a JavaScript library?
No. There’s no dependency: the code is plain HTML and CSS plus a three-line script that updates one CSS variable.
Can I put several sliders on one page?
Yes. The script attaches to every slider with the same class name, so add as many as you like.
Are my images uploaded?
No. The preview uses the images straight from your device.