Before/after sliders are everywhere: photo editors, retouching portfolios, image tools. Most tutorials reach for a library, but a good one fits in about 40 lines of HTML, CSS and JavaScript using clip-path and a range input. It is accessible, works with touch, and has no dependencies.
The idea
Stack two images exactly on top of each other. Show the "after" image fully, and clip the "before" image so only its left part is visible. Moving the slider changes how much of the "before" image you see.
Markup
<div class = "compare" style = "--pos: 50%" > <img src = "after.jpg" alt = "Edited image" class = "after" > <img src = "before.jpg" alt = "Original image" class = "before" > <input type = "range" min = "0" max = "100" value = "50" aria-label = "Reveal original image" class = "slider" > <span class = "handle" aria-hidden = "true" ></span> </div>
Enter fullscreen mode
Exit fullscreen mode
Using a real <input type="range"> gives us keyboard support and screen-reader semantics for free.
CSS
.compare {…