Menu

A Dependency-Free Before/After Image Slider With clip-path and a Range Input
📰

A Dependency-Free Before/After Image Slider With clip-path and a Range Input

DEV Community: css·M Paracha·10 days ago
#JkJTC0pv
#devtocss#devto#dev#slider#compare
Long article (628 words) — no AI summary yet.
Reading 0:00
15s threshold

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 {…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.