The simplest accessible before and after slider stacks the two images in the same grid cell, trims the top image with clip-path: inset() controlled by a CSS custom property, and puts a full-size <input type="range"> over both. Dragging the range updates the property, so a three-line script is all the JavaScript you need, and the slider works with a mouse, touch and keyboard out of the box. The before and after slider builder generates this code with your own images.
Key takeaways
- Stack both images with CSS grid; clip the “after” image with
clip-path: inset(0 0 0 var(--pos)). - A real range input gives you keyboard, touch and screen reader support without a library.
- Both images need alt text, and the two must have identical dimensions and framing.
- Give the handle a visible focus style so keyboard users can see where they are.
How does a before and after slider work?
Both images occupy the same space, one on top of the other. The top (“after”) image is clipped from the left by a percentage. At 50%, you see the left half of the before image and the right half of the after image. Moving the slider changes that percentage, revealing more of one or the other. A thin line and a handle drawn at the same position make the split obvious.
The HTML, CSS and JavaScript
<div class="ba" style="--pos: 50%">
<img src="before.jpg" alt="Homepage before the redesign" width="1200" height="800">
<img class="ba__after" src="after.jpg" alt="Homepage after the redesign" width="1200" height="800">
<input class="ba__range" type="range" min="0" max="100" value="50"
aria-label="Compare before and after">
</div>
.ba { display: grid; position: relative; }
.ba > * { grid-area: 1 / 1; }
.ba img { display: block; width: 100%; height: auto; }
.ba__after { clip-path: inset(0 0 0 var(--pos)); }
.ba::after { /* the divider line */
content: "";
position: absolute;
inset: 0 auto 0 var(--pos);
width: 2px;
background: #fff;
pointer-events: none;
}
.ba__range {
appearance: none;
margin: 0;
background: none;
opacity: 0; /* invisible, but still the control */
cursor: ew-resize;
}
.ba:has(.ba__range:focus-visible) {
outline: 3px solid #2f8cff;
outline-offset: 4px;
}
document.querySelectorAll('.ba__range').forEach(range => {
range.addEventListener('input', () =>
range.parentElement.style.setProperty('--pos', range.value + '%'));
});
The range input fills the whole slider, so clicking or dragging anywhere moves the split, and arrow keys move it in steps once it has focus. The script attaches to every slider with the same class, so several on one page all work.
Why use a range input instead of a custom drag handle?
| Behaviour | Range input | Custom div handle |
|---|---|---|
| Mouse and touch | Built in | Needs pointer event code |
| Keyboard | Arrow keys, Home and End built in | Needs key handlers |
| Screen readers | Announced as a slider with its value | Needs ARIA roles and value updates |
| Focus | Focusable by default | Needs tabindex |
| Code size | Three lines of script | Often a library |
Native controls carry years of browser accessibility work for free. Rebuilding them with divs usually leaves something out, most often keyboard support.
Focus: Because the range input is invisible, the browser’s own focus ring is invisible too. The :has() rule above draws an outline around the whole slider when the input has keyboard focus. Keep something like it, so keyboard users can see the control.
How do you make a top and bottom split?
Clip from the top instead of the left, and turn the range input vertical. Current browsers support vertical range inputs through writing-mode, so it still works with the arrow keys. Move the divider line to top: var(--pos) and make it 2px tall and full width.
.ba--vertical .ba__after { clip-path: inset(var(--pos) 0 0 0); }
.ba--vertical .ba__range { writing-mode: vertical-lr; cursor: ns-resize; }
Making both images line up
The comparison only works if the two images have exactly the same dimensions, crop and alignment. If one is even a few pixels off, details jump as you drag and the effect looks broken.
- Redesigns: take both screenshots at the same browser width and zoom level. The screenshot mockup generator can frame them consistently.
- Photo edits: export both from the same file at the same size.
- Compression tests: show the original and the compressed file side by side to prove there is no visible difference; see our guide to compressing images.
- Resize both together with the bulk image resizer if they come from different sources.
What are good uses for a before and after slider?
They suit any change that is easiest to understand by looking: a website redesign, photo retouching, colour grading, image compression quality, renovation and landscaping projects, cleaning and restoration work, or a dark theme against a light one. Keep the before image honest. A slider that compares a deliberately poor “before” with a polished “after” is quickly spotted by visitors.
Does a slider hurt page speed?
Each slider loads two full images, so size them properly and lazy-load sliders that sit below the fold with loading="lazy". Include width and height attributes so the browser reserves space and nothing shifts while they load. Beyond that, the code is tiny: a few lines of CSS and three of JavaScript, with no dependencies.
Build one with your own images
Drop your two images into the before and after slider builder, choose a left–right or top–bottom split, set the labels and handle colour, and copy the code, or download a standalone demo page. Your images stay on your device while you preview.
Last checked: October 2026.
Sources: MDN: <input type=”range”>, MDN: inset(), MDN: :has().


