Basic Image Comparison

Simple before/after with two images. Drag the divider or use arrow keys to compare. Default 16:9 aspect ratio with center-positioned divider.

Before
After
<BeforeAfter
  beforeImage="https://example.com/before.jpg"
  afterImage="https://example.com/after.jpg"
/>

Labeled Comparison

Images with "Before" and "After" labels. Custom divider styling with thicker line and larger handle for better mobile interaction.

Before
Before
After
After
<BeforeAfter
  beforeImage="https://example.com/before.jpg"
  afterImage="https://example.com/after.jpg"
  beforeLabel="Before"
  afterLabel="After"
  dividerWidth={3}
  handleSize={56}
/>

Initial Position Preset

Divider starts at 25% (mostly showing "after"). Useful for highlighting improvements. onChange callback tracks current position: 50%

Before
Original
After
Enhanced
<BeforeAfter
  beforeImage="https://example.com/before.jpg"
  afterImage="https://example.com/after.jpg"
  beforeLabel="Original"
  afterLabel="Enhanced"
  aspectRatio="1/1"
  initialPosition={25}
  onChange={(pos) => console.log('Position:', pos)}
/>

Styled Variant

Custom colours for divider and handle. Square aspect ratio perfect for product photos. Demonstrates full customisation capabilities.

Before
Low Quality
After
High Quality
<BeforeAfter
  beforeImage="https://example.com/before.jpg"
  afterImage="https://example.com/after.jpg"
  beforeLabel="Low Quality"
  afterLabel="High Quality"
  aspectRatio="1/1"
  dividerColor="#3b82f6"
  dividerWidth={4}
  handleColor="#3b82f6"
  handleSize={60}
/>

Key Features

🎨

CSS-Based Clipping

GPU-accelerated clip-path for smooth image reveals. No Canvas manipulation required.

πŸ–±οΈ

Unified Input

Pointer Events API handles mouse, touch, and pen input seamlessly.

⌨️

Keyboard Navigation

Arrow keys for precise control. Full ARIA labels for screen readers.

🎯

Customisable

Aspect ratio, divider styling, labels, and handle size all configurable.

πŸ“±

Responsive

Works perfectly on mobile with touch support. Adapts to viewport size.

⚑

Zero Dependencies

Pure CSS and Svelte. No external libraries. Copy-paste ready.

Props Reference

PropTypeDefaultDescription
beforeImagestringrequiredURL for before image
afterImagestringrequiredURL for after image
beforeAltstring'Before'Alt text for before image
afterAltstring'After'Alt text for after image
beforeLabelstring-Optional label shown on before side
afterLabelstring-Optional label shown on after side
aspectRatiostring'16/9'CSS aspect ratio (e.g., '1/1', '4/3')
widthnumber | string'100%'Container width (px or %)
initialPositionnumber50Initial divider position (0-100)
disabledbooleanfalseDisable dragging interaction
dividerColorstring'#ffffff'Divider line colour (hex)
dividerWidthnumber2Divider line width in pixels
handleSizenumber48Handle circle size in pixels
handleColorstring'#ffffff'Handle background colour (hex)
onChange(pos: number) => void-Callback fired when position changes
classstring''Additional CSS classes

Use Cases

πŸ› οΈ Product Improvements

Show product evolution by comparing old vs. new versions. Perfect for highlighting upgrades and improvements.

🎨 Image Editing

Demonstrate before/after photo editing or filter effects. Showcase your editing skills or filter presets.

πŸ’Ž Design Iterations

Compare design versions to visualize changes. Great for presenting redesigns to clients or stakeholders.

βš–οΈ Quality Comparisons

Show differences between standard vs. premium versions, compression levels, or quality tiers.

πŸ”¬ A/B Testing

Visual comparison of variants for marketing campaigns, landing pages, or UI changes.

🏑 Real Estate

Renovation before/after photos. Perfect for showcasing property transformations and improvements.

Technical Implementation

CSS Clip-Path

Uses CSS clip-path: inset() for GPU-accelerated clipping. No Canvas manipulation required for smooth 60fps performance.

Pointer Events API

Unified handling for mouse, touch, and pen input. Pointer capture ensures smooth dragging even when cursor leaves element.

Svelte 5 Runes

Uses $state() and $derived() for reactive state management. Automatic reactivity with minimal boilerplate.

Accessibility

Full keyboard support with arrow keys. ARIA roles, labels, and live regions for screen readers. Respects reduced motion preferences.