Guide
Adding a before and after image slider with plain HTML and CSS
A before and after slider shows a change by letting the visitor drag a handle across two pictures. It suits renovations, retouching, restorations and product improvements. Many ready-made sliders are plugins that load scripts and libraries. This generator writes a small block of HTML and CSS that does the same job with a single range input.
How it works
Two images are stacked in a box of the same size. The top image is clipped with clip-path: inset(0 0 0 var(--pos)), which hides the left part up to the position and reveals the image below. A range input covers the box with zero opacity, so the visitor drags anywhere and the value changes. The input’s oninput attribute copies its value into the --pos variable on the container, which moves the clip and the white divider line.
What to enter
Give the address of the before image and of the after image, as you will use them on your site, such as images/before.jpg. Add alt text for each that describes what the picture shows. Set the left and right labels, which default to Before and After, the starting position from 0 to 100 and the aspect ratio, such as 16 / 9. Both images should have the same proportions so that features line up when the divider moves.
The preview
To try the slider before you upload anything, choose files from your device in the preview fields. They are used only to draw the preview here, so the code keeps the addresses you typed. If you type an https address, your browser loads that image from its own site to draw the preview. Drag across the preview to see how it behaves, and use “Clear files” to reset.
Accessibility and loading
Alt text is the most important part for accessibility and search, so the tool warns when either is empty. The range input is real, so the keyboard works. For speed, use images of a sensible size and format, such as WebP or optimised JPEG, and consider adding loading="lazy" if the slider is far down the page.
A worked example
The defaults describe a living room: before images/before.jpg with the alt text “Living room before the renovation”, after images/after.jpg with “Living room after the renovation”, labels Before and After, a start of 50 and a 16 / 9 ratio. The output has a div with the class ba-slider and --pos: 50%, two img elements, two labels, a divider and the range input, followed by a style block with the rules. Dragging the preview to 20% changes the variable to 20% and moves the divider.
Limits
The snippet uses one inline event handler, so sites with a strict content security policy need to move it into a script file. It supports one slider per snippet and does not include touch-specific tuning, vertical sliders or labels that follow the divider. The tool does not host images or check that the addresses work. Test the finished slider on a phone as well as a computer.
Choosing good pictures
Take both photos from the same position, with the same framing and lighting, so that the only difference is the change you want to show. Crop them to the same size before you add them. A subtle change shows better when the viewer can drag slowly, so start the slider near the middle and label the sides clearly.
Related tools
- CTA Button Generator writes another ready-to-paste snippet.
- Browser Mockup Frame places a screenshot in a device frame.
- Image Compressor shrinks the pictures before you add them to the page.
- Image Resizer gives both pictures the same proportions.
Checked against the current version of the Before & After Slider Generator on .

