Browse components
Before and After SliderNo account needed
Before and after image slider with a draggable divider, touch and keyboard support, and optional labels.
Add to your project
Let your AI assistant add it
Paste this into your AI coding tool to add the component.
# Integrate this UI component
Add this component to my project.
1. Install any missing dependencies.
2. Make sure Tailwind CSS is set up and scans the new file.
3. Prefer passing props over editing the component source — changing the component itself may break it.
4. Adjust colors, size, and layout via props or wrapper classes to match my project. You know my codebase better than this snippet does.
## Component source
```tsx
'use client';
import { useState, type KeyboardEvent, type PointerEvent } from 'react';
type SliderImage = {
src: string;
alt: string;
className?: string;
};
type BeforeAfterSliderProps = {
before: SliderImage;
after: SliderImage;
beforeLabel?: string;
afterLabel?: string;
defaultPosition?: number;
className?: string;
};
const clamp = (value: number) => Math.min(100, Math.max(0, value));
export default function BeforeAfterSlider({
before,
after,
beforeLabel = 'Before',
afterLabel = 'After',
defaultPosition = 50,
className = '',
}: BeforeAfterSliderProps) {
const [position, setPosition] = useState(clamp(defaultPosition));
const moveTo = (event: PointerEvent<HTMLElement>) => {
const rect = event.currentTarget.getBoundingClientRect();
setPosition(clamp(((event.clientX - rect.left) / rect.width) * 100));
};
const onPointerDown = (event: PointerEvent<HTMLElement>) => {
if (event.button !== 0) return;
event.currentTarget.setPointerCapture(event.pointerId);
moveTo(event);
};
const onPointerMove = (event: PointerEvent<HTMLElement>) => {
if (event.currentTarget.hasPointerCapture(event.pointerId)) moveTo(event);
};
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
const step = event.shiftKey ? 10 : 1;
const next = {
ArrowLeft: position - step,
ArrowDown: position - step,
ArrowRight: position + step,
ArrowUp: position + step,
Home: 0,
End: 100,
}[event.key];
if (next === undefined) return;
event.preventDefault();
setPosition(clamp(next));
};
return (
<figure
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
className={[
'relative isolate w-full cursor-ew-resize touch-pan-y select-none overflow-hidden rounded-2xl bg-[var(--color-muted,#f3f4f6)]',
className,
]
.filter(Boolean)
.join(' ')}
>
<img
src={after.src}
alt={after.alt}
draggable={false}
className={['block h-full w-full object-cover', after.className].filter(Boolean).join(' ')}
/>
<img
src={before.src}
alt={before.alt}
draggable={false}
className={['absolute inset-0 h-full w-full object-cover', before.className].filter(Boolean).join(' ')}
style={{ clipPath: `inset(0 ${100 - position}% 0 0)` }}
/>
{beforeLabel && (
<span className="pointer-events-none absolute left-3 top-3 rounded-full bg-black/60 px-3 py-1 text-xs font-medium text-white">
{beforeLabel}
</span>
)}
{afterLabel && (
<span className="pointer-events-none absolute right-3 top-3 rounded-full bg-black/60 px-3 py-1 text-xs font-medium text-white">
{afterLabel}
</span>
)}
<div
className="absolute inset-y-0 w-0.5 -translate-x-1/2 bg-white shadow-[0_0_8px_rgba(0,0,0,0.4)]"
style={{ left: `${position}%` }}
>
<div
role="slider"
tabIndex={0}
aria-label={`Compare ${beforeLabel || 'before'} and ${afterLabel || 'after'} images`}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(position)}
aria-valuetext={`${Math.round(position)}% ${beforeLabel || 'before'}`}
onKeyDown={onKeyDown}
className="absolute left-1/2 top-1/2 grid size-10 -translate-x-1/2 -translate-y-1/2 place-items-center rounded-full bg-white text-gray-900 shadow-lg outline-none focus-visible:ring-2 focus-visible:ring-[color:var(--color-ring,#111827)] focus-visible:ring-offset-2"
>
<svg aria-hidden="true" viewBox="0 0 24 24" className="size-5" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<path d="m9 6-6 6 6 6M15 6l6 6-6 6" />
</svg>
</div>
</div>
</figure>
);
}
```
Works with your AI tools
* Compatible with most projects that use the default React/Next.js framework.
Built With
Props
| Prop | Type | Description |
|---|---|---|
before | { src: string; alt: string; className?: string } | Image shown on the left of the divider |
after | { src: string; alt: string; className?: string } | Image shown on the right of the divider |
beforeLabel | string | Label on the before side; empty string hides it |
afterLabel | string | Label on the after side; empty string hides it |
defaultPosition | number | Initial divider position from 0 to 100 |
className | string | Styles for the outer frame, such as aspect ratio and width |
More Gallery Components
3D coverflow carousel with keyboard, dot nav, and optional autoplay that pauses on hover.

Premium animated image and content slider with directional transitions, swipe gestures, captions, and numbered progress.
Fanned photo arch with center-forward stacking; hover lifts a card and scales it up.

Responsive horizontal carousel with native scrolling, scroll snapping, and accessible previous and next controls.






