Copied!
Guide

How to make a before and after image slider

Two stacked images, a clip-path and a range input. Using a real range input gives you touch, drag and keyboard support for free. Here is the whole thing in about thirty lines.

← Blog
Title card: Before / after slider. Preview of the Image compare scroll element from the Enrichr Library.

December 4, 2026

3 min readOne line of JavaScript

To make a before and after image slider, stack two images in the same place, clip the top one with clip-path, and control the clip with a range input laid over both. Because the control is a real range input, it works with a mouse, a finger and the arrow keys with no drag code at all.

Try it on any site. The free animation library checker shows you which animation libraries actually run on a page (GSAP, Lenis, Framer Motion). Paste a URL, no sign-up for your first lookup.

What is the code?

<div class="compare" style="--pos: 50%">
  <img src="before.jpg" alt="The room before the redesign">
  <img src="after.jpg" alt="The room after the redesign" class="top">
  <input type="range" min="0" max="100" value="50"
         aria-label="Show more of the after image">
</div>
.compare { display: grid; position: relative; max-width: 800px; }
.compare > * { grid-area: 1 / 1; }
.compare img { width: 100%; aspect-ratio: 3 / 2; object-fit: cover; display: block; }
.compare .top { clip-path: inset(0 calc(100% - var(--pos)) 0 0); }

/* the divider line */
.compare::after {
  content: ""; grid-area: 1 / 1; justify-self: start;
  width: 2px; margin-left: calc(var(--pos) - 1px);
  background: #fff; pointer-events: none;
}

/* the control: full size, invisible, on top */
.compare input {
  width: 100%; height: 100%; margin: 0;
  opacity: 0; cursor: ew-resize;
}
.compare:focus-within { outline: 2px solid #1db954; outline-offset: 2px; }
document.querySelectorAll(".compare input").forEach((input) => {
  input.addEventListener("input", () =>
    input.parentElement.style.setProperty("--pos", input.value + "%"));
});

How does it work?

All three children share one grid cell, so they sit on top of each other with no absolute positioning. The variable --pos is the divider position. inset(0 calc(100% - var(--pos)) 0 0) cuts the top image from the right edge inwards, so the "after" picture shows to the left of the divider and the "before" picture to the right.

Why a range input?

A hand-written drag handle needs pointer events, touch handling, capture, keyboard listeners and ARIA roles, and most demos skip half of them. A range input already has all of it. It is invisible here, but it still takes focus, so the outline rule on the wrapper shows keyboard users where they are.

How do I add a round handle?

Add another element in the same grid cell, position it at left: var(--pos), and give it pointer-events: none so clicks pass through to the range input beneath.

Can the divider follow the scroll?

Yes. Work out how far the slider has travelled through the window as a number from 0 to 1 and write that into --pos. Keep the range input too, so the visitor can still take over by hand.

Which finished example can I copy?

Image compare on scroll in the free Enrichr Library is a before and after slider whose divider is scrubbed by scroll position and can also be dragged or moved with the keyboard. It comes with live controls, the code to copy and a pen on CodePen. Related elements:

Where to go next

FAQ

How do I make a before and after slider without a library?

Place two images of the same size on top of each other, clip the top one with clip-path: inset(0 X 0 0), and change X from a range input. That is all a comparison slider is.

Why use clip-path instead of changing the width?

Changing the width of a wrapper makes the browser lay the image out again and can make it shift or rescale. clip-path only changes what is painted, so it is smoother and the image never moves.

How do I make the slider work with the keyboard?

Use a native range input as the control. It can be focused with Tab and moved with the arrow keys by default. Give it an aria-label such as "Show more of the after image".

Do the two images have to be the same size?

They should have the same aspect ratio. Give both width: 100%, the same aspect-ratio and object-fit: cover so they line up exactly.