Image Comparison Slider

Interactive before/after image comparisons with draggable sliders


Image Comparison Slider

Create interactive before/after image comparisons with a draggable slider, perfect for showcasing design changes, UI improvements, or visual transformations.

Basic Syntax

Use :::image-compare with two images:

Before
An image used for comparison
Before
An image used for comparison

Syntax Structure

:::image-compare
![Before description](path/to/before.jpg)
![After description](path/to/after.jpg)
:::
Markdown

The first image is the "before" view and the second is the "after" view. Users can drag the slider to reveal more of either image.

With Custom Label

Add a descriptive label above the comparison:

Design Evolution
Original Design
Redesigned Interface
Original Design
Redesigned Interface

Common Use Cases

1. UI/UX Design Changes

Showcase interface improvements and redesigns:

Homepage Redesign
Before
An image used for comparison
Before
An image used for comparison

Perfect for:

  • Landing page redesigns
  • Dashboard improvements
  • Component library updates
  • Branding changes

2. Code Output Comparisons

Compare visual results of code changes:

CSS Grid vs Flexbox Layout
Flexbox Implementation
CSS Grid Implementation
Flexbox Implementation
CSS Grid Implementation

Useful for:

  • CSS technique comparisons
  • Rendering differences
  • Performance optimizations
  • Browser compatibility

3. Image Processing Results

Show before/after effects of image manipulation:

Image Optimization
Original (2.4MB)
Optimized (245KB)
Original (2.4MB)
Optimized (245KB)

Great for demonstrating:

  • Compression techniques
  • Filter applications
  • Resolution changes
  • Format conversions

4. Bug Fixes

Illustrate visual bug corrections:

Layout Bug Fix
Broken Layout
Fixed Layout
Broken Layout
Fixed Layout

5. Responsive Design

Compare mobile vs desktop layouts:

Mobile vs Desktop View
Mobile Layout
Desktop Layout
Mobile Layout
Desktop Layout

6. Accessibility Improvements

Show contrast and readability enhancements:

Accessibility Enhancement
Low Contrast
WCAG AA Compliant
Low Contrast
WCAG AA Compliant

Features

Interactive Controls

  • Mouse Drag: Click and drag the slider handle
  • Touch Support: Swipe on mobile devices
  • Smooth Animation: Fluid slider movement
  • Visual Feedback: Handle indicates interactivity

Responsive Design

The comparison adapts to:

  • Different screen sizes
  • Container widths
  • Image aspect ratios
  • Mobile and desktop devices

Accessibility

Built with accessibility in mind:

  • Alt text from markdown images
  • Keyboard navigation support
  • Screen reader friendly
  • Touch-optimized controls

Visual Indicators

  • Before/After labels on images
  • Central slider handle with icon
  • Hover effects on interactive elements
  • Clear visual separation line

In Documentation

Tutorial Progression

Show steps in a learning process:

steps

Step 1: Initial Setup

Start with the basic structure:

Initial Setup
Before
An image used for comparison
Before
An image used for comparison

Step 2: Add Styling

Apply CSS improvements:

Styled Version
Before
An image used for comparison
Before
An image used for comparison

Step 3: Final Polish

Complete the design:

Final Result
Before
An image used for comparison
Before
An image used for comparison

With Explanatory Asides

Multiple Comparisons

Create a series of comparisons to show incremental changes:

Version 1.0 to 2.0

Version 1.0
Before
An image used for comparison
Before
An image used for comparison

Version 2.0 to 3.0

Version 2.0
Before
An image used for comparison
Before
An image used for comparison

Version 3.0 to Current

Current Version
Before
An image used for comparison
Before
An image used for comparison

Best Practices

Use High-Quality Images

Good:

  • Clear, well-lit images
  • Consistent dimensions
  • Appropriate resolution
  • Same aspect ratio

Avoid:

  • Blurry or pixelated images
  • Different sizes or ratios
  • Inconsistent lighting
  • Misaligned content

Descriptive Alt Text

Good:

![Dashboard with cluttered navigation and small text](before.jpg)
![Streamlined dashboard with clear hierarchy and readable fonts](after.jpg)
Markdown

Generic:

![Before](before.jpg)
![After](after.jpg)
Markdown

Meaningful Labels

Good:

:::image-compare[Performance Optimization - 60% Faster Load Time]
Markdown

Vague:

:::image-compare[Comparison]
Markdown

Align Your Images

Ensure both images show the same:

  • Camera angle/perspective
  • Content alignment
  • Zoom level
  • Framing

This makes the comparison meaningful and easy to understand.

Technical Details

Image Requirements

  • Format: Any web-compatible format (JPG, PNG, SVG, WebP, GIF)
  • Size: Ideally under 1MB each for performance
  • Dimensions: Both images should have the same dimensions
  • Aspect Ratio: Must be identical for proper alignment

Browser Support

Works in all modern browsers:

  • Chrome/Edge (latest)
  • Firefox (latest)
  • Safari (latest)
  • Mobile browsers

Performance

The component is optimized for:

  • Fast initial load
  • Smooth drag interactions
  • Minimal JavaScript overhead
  • Efficient rendering

Self-Contained

All styling and functionality is:

  • Inline CSS (no external stylesheets)
  • Inline JavaScript (no dependencies)
  • No framework requirements
  • Works everywhere markdown renders

Syntax Reference

:::image-compare
![Before](path/to/before.jpg)
![After](path/to/after.jpg)
:::
Markdown

With label:

:::image-compare[Your Custom Label]
![Before](before.jpg)
![After](after.jpg)
:::
Markdown

Parameters:

  • label (optional) - Text displayed above the comparison
  • First image - The "before" image (left side)
  • Second image - The "after" image (right side, initially hidden)

Error Handling

If images are missing or invalid:

:::image-compare
Some text without images
:::
Markdown

An error message will display indicating that both before and after images are required.

Real-World Examples

Portfolio Case Study

Project: E-commerce Redesign

Homepage Transformation
Before
An image used for comparison
Before
An image used for comparison

Results:

  • 45% increase in conversions
  • 30% decrease in bounce rate
  • Improved mobile experience

Blog Post Example

When we started the redesign, user feedback indicated that the interface felt cluttered and overwhelming:

Before and After User Interface
Original overcrowded interface
Clean, modern interface with better spacing
Original overcrowded interface
Clean, modern interface with better spacing

The new design follows material design principles and significantly improves the user experience.

Documentation Example

Upgrading from v1 to v2

The new version introduces a completely redesigned interface:

Version Comparison
Version 1.x
Version 2.x
Version 1.x
Version 2.x

See the migration guide below for upgrading instructions.

Comparison with Alternatives

vs. Side-by-Side Images

Image Compare Slider:

  • Interactive exploration
  • Space-efficient
  • Focuses attention on differences
  • Better for subtle changes

Side-by-Side:

  • Shows both images simultaneously
  • No interaction required
  • Better for completely different images
  • Requires more space

vs. GIF/Video

Image Compare Slider:

  • User controls the comparison
  • Better performance
  • Accessible to all users
  • Easier to implement

GIF/Video:

  • Shows changes over time
  • No interaction needed
  • Larger file sizes
  • May have accessibility issues

Tips for Great Comparisons

Photography Tips

  1. Use a Tripod - Keep the camera position identical
  2. Same Lighting - Maintain consistent lighting conditions
  3. Lock Focus - Don't let autofocus change between shots
  4. Same Settings - Use identical camera settings

Screenshot Tips

  1. Same Zoom Level - Browser zoom should be identical
  2. Same Window Size - Keep viewport dimensions the same
  3. Same Scroll Position - Align content vertically
  4. Hide Dynamic Content - Remove timestamps or changing data

Design Tips

  1. Highlight Key Changes - Make differences obvious
  2. Keep Context - Don't crop too tightly
  3. Use Annotations - Consider adding arrows or highlights
  4. Tell a Story - Show meaningful improvements
  • Tabs - For non-visual comparisons
  • Details - For expandable content
  • Cards - For presenting multiple examples
  • Link Cards - For external resources

Troubleshooting

Slider Doesn't Appear

Cause: Missing or invalid images Solution: Ensure both images are properly formatted markdown images

Images Don't Align

Cause: Different image dimensions Solution: Resize images to have identical width and height

Slider Doesn't Drag Smoothly

Cause: Images are too large Solution: Optimize and compress your images

Images Load Slowly

Cause: Large file sizes Solution: Use WebP format and compress images

Advanced Usage

Combining with Code Examples

Show how code changes affect the visual output:

CSS Changes:

/* Before: Flexbox */
.container {
  display: flex;
  flex-wrap: wrap;
}
CSS
/* After: CSS Grid */
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
CSS

Visual Result:

Layout Comparison
Flexbox Layout
CSS Grid Layout
Flexbox Layout
CSS Grid Layout

In Card Grids

UI Redesign
Before
An image used for comparison
Before
An image used for comparison

Complete interface overhaul with modern design principles.

Performance Optimization
Before
An image used for comparison
Before
An image used for comparison

Reduced load time by 60% through image optimization.


Back to Markdown Features