Before/after slider
Also called image comparison slider, comparison slider, or before and after slider
A comparison control that stacks two versions of an image and lets people drag a divider to reveal more of one or the other.
import { useState } from 'react';
import { Slider } from '@base-ui/react/slider';
const LABEL =
'absolute top-3 rounded-full bg-black/60 px-2.5 py-1 text-xs font-medium text-white';
export function BeforeAfterSlider() {
const [position, setPosition] = useState(50);
return (
<div className='relative aspect-video w-full max-w-2xl overflow-hidden rounded-2xl select-none'>
<img
src='/kitchen-after.jpg'
alt='Kitchen after the renovation'
className='absolute inset-0 size-full object-cover'
/>
{/* Clip the top image instead of resizing it, so the two stay perfectly aligned. */}
<img
src='/kitchen-before.jpg'
alt='Kitchen before the renovation'
className='absolute inset-0 size-full object-cover'
style={{ clipPath: `inset(0 ${100 - position}% 0 0)` }}
/>
<span className={`${LABEL} left-3`}>Before</span>
<span className={`${LABEL} right-3`}>After</span>
{/* Base UI handles dragging anywhere on the image, touch, and the arrow keys. */}
<Slider.Root
value={position}
onValueChange={setPosition}
className='absolute inset-0'
>
<Slider.Control className='size-full cursor-ew-resize touch-none'>
<Slider.Track className='size-full'>
<Slider.Thumb
aria-label='Comparison position'
className='h-full w-0.5 bg-white has-focus-visible:outline-2 has-focus-visible:outline-offset-2 has-focus-visible:outline-white'
>
<span className='absolute top-1/2 left-1/2 grid size-11 -translate-x-1/2 -translate-y-1/2 place-items-center rounded-full bg-white text-black shadow-lg'>
↔
</span>
</Slider.Thumb>
</Slider.Track>
</Slider.Control>
</Slider.Root>
</div>
);
}Build a before/after image comparison slider: stack the two images, clip the top one with clip-path: inset(0 calc(100% - var(--position)) 0 0), and start the divider at 50%. Support mouse and touch dragging anywhere on the image, a 44px round handle, "Before" and "After" labels, and Left and Right arrow keys that move the divider by 5%.
Paste into Layerwise or any AI coding tool.
What is Before/after slider?
A before/after slider places two versions of the same picture on top of each other. A vertical divider with a round handle sits in the middle, and dragging it clips the top image so more of the bottom one shows. People compare details directly instead of flipping between two pictures.
It suits photo edits, redesigns, renovations, cleaning and beauty results, image upscalers, and maps that change over time. Both images need the same size and framing, or the comparison falls apart.
Clip the top image with clip-path: inset() driven by the divider position, which is cheaper than resizing it. Let people drag anywhere on the image, keep the handle at least 44px wide for touch, and make it keyboard accessible: a native range input, or an element with role="slider" that responds to the arrow keys.
Related terms
All termsClip-path
A CSS property that cuts an element to a shape, such as a circle, an inset rectangle, or a polygon, and hides everything outside it.
Drag gesture
Moving an element by pressing on it and sliding the pointer or finger, usually within set bounds and with a glide after release.
Mask
A CSS technique that uses an image or gradient to set how visible each part of an element is, often used to fade edges softly.
Crossfade
A transition where the old content fades out while the new content fades in at the same spot, so the two briefly overlap.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.
