Before/After Image Slider
Draggable tailwind before after slider for image comparison.
<div class="relative w-full max-w-xl mx-auto rounded-2xl overflow-hidden select-none aspect-video">
<div class="absolute inset-0 bg-gradient-to-br from-slate-700 to-slate-900"></div>
<div class="absolute inset-0 bg-gradient-to-br from-amber-300 to-orange-500" style="clip-path: inset(0 50% 0 0);"></div>
<span class="absolute top-3 left-3 px-2 py-1 rounded-md bg-black/50 text-white text-xs font-bold">Before</span>
<span class="absolute top-3 right-3 px-2 py-1 rounded-md bg-black/50 text-white text-xs font-bold">After</span>
<div class="absolute inset-y-0 left-1/2 w-0.5 bg-white -translate-x-1/2"></div>
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-10 h-10 rounded-full bg-white shadow-lg flex items-center justify-center">
<svg class="w-5 h-5 text-slate-700" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M8 7l-5 5 5 5M16 7l5 5-5 5"/></svg>
</div>
</div>Free to use in personal and commercial projects. No attribution required.
Variants
Same component, different looks. Preview each one live and copy the code you like.
Vertical
Top and bottom comparison for portrait images and mobile screenshots.
30 percent
Off center split that teases more of the after image.
Square
Sharp corners and teal tones for technical docs and dashboards.
About this before/after image slider
A tailwind before after slider lets visitors drag a divider to compare two images: before on one side, after on the other. This image comparison slider html is the classic demo for photo editors, renovation portfolios, and AI enhancement tools. It turns a static claim into proof the visitor controls.
The technique stacks two full-size layers and clips the top one with clip-path inset, revealing exactly half. A white divider line with a circular handle sits at the seam, and corner labels mark which side is which. The static preview shows the 50 percent position; wiring it as a drag comparison slider takes a few lines of JavaScript that update the clip-path on pointer drag.
Use it only when the comparison is genuinely dramatic; subtle changes disappoint. Keep both images the same dimensions so the seam aligns perfectly. Add before after image css labels with high-contrast chips so the meaning survives even if the handle is missed.
Claims do not convince; control does. Telling visitors your tool enhances photos is just words, and words get discounted. Handing them the divider and letting them drag it makes the proof theirs: they revealed the difference themselves. That ownership of the conclusion is why interactive comparisons outperform static before and after images placed side by side.
Two full-size images stack in the same frame, and the top one gets clipped with the inset function so only the left portion shows. One custom property, --pos, drives everything: the clip amount, the divider line position, and the handle location. Because a single variable controls all three, they can never desynchronize. Dragging updates --pos, and the whole seam follows as one unit.
Start the divider away from center, around 30 percent, for a more dramatic first impression than a neat half split. Label both sides with high-contrast chips so the meaning survives even if the handle is missed. Make the handle at least 44px so touch dragging feels precise rather than fiddly. Use identical aspect ratios with object-cover so any source images get normalized to the same frame.
The mistake is a handle with no affordance: a thin line users don't realize they can drag. Without a visible grip, the slider reads as a static divider and the interaction goes undiscovered. Give the handle a chunky circular grip with arrows, add a subtle wiggle animation on first load to hint draggability, and label both sides. Discoverability is the whole game; the drag physics don't matter if nobody drags.
Where to use it
- ✓ Photo editing tools
- ✓ Renovation portfolios
- ✓ AI image enhancers
- ✓ Product restoration demos
Customization tips
Change the clip percentage to start at 30 percent for a more dramatic reveal. Swap the gradient placeholders for real before and after images with identical aspect ratios.
Design notes
When to use
Before and after sliders are the classic demo for transformations: photo edits, home renovations, filters, and restoration work. Letting visitors drag the divider themselves is more convincing than any written claim, because they control the reveal. They work best with two images shot from the same angle so the comparison is honest.
What to avoid
Do not compare images with different aspect ratios or crops, because a mismatched comparison misleads even when you do not mean it to. Avoid leaving the divider without before and after labels, since visitors should not have to guess which side is which. And do not make the handle a tiny target, because dragging precision matters on touch screens.
Accessibility
Make the divider keyboard operable with arrow keys, exposing it as a slider role with aria-valuenow for the position. Provide text labels for before and after rather than relying on visual placement alone.
Frequently asked questions
How do I make a tailwind before after slider draggable?
Add a pointermove listener on the container that sets the top layer's clip-path inset based on the cursor's x position as a percentage. Update the divider's left position to match.
Can a before after slider work without JavaScript?
Only as a static 50/50 comparison. The drag interaction needs JavaScript to update the clip-path, but the layout itself is pure CSS.
What image sizes work best for an image comparison slider?
Both images must share the exact same dimensions and aspect ratio, otherwise the seam will not align and the comparison looks broken.
How does the clip-path technique work in a before after slider?
The top image gets clip-path: inset(0 calc(100% - var(--pos)) 0 0), which hides everything right of the divider position. Dragging updates the --pos custom property, and the divider line and handle are positioned at the same percentage. One variable drives the image reveal, the line, and the handle together.
How do I make a before after slider keyboard accessible?
Drive the slider with a real input type="range" overlaid invisibly across the component. Arrow keys then move the divider natively, and screen readers announce the value as it changes. Give it an aria-label like "Reveal after image" so its purpose is clear out of context.
Should before and after images be the same dimensions?
Yes, identical aspect ratio and framing. Mismatched sizes break the comparison illusion because features will not line up as you drag. Set aspect-video or aspect-square on the wrapper with object-cover on both images so any source dimensions are normalized.
Related elements
Marquee Ticker
Infinite scrolling logo/text ticker, pure CSS.
2024
Launched v1.0
First public release with core features.
2025
Reached 10k users
Community growth milestone.
2026
What's next
Roadmap in progress.
Vertical Timeline
Clean vertical timeline with dots and connecting line.
What is your return policy? +
You can return any item within 30 days of purchase for a full refund. No questions asked.
How long does shipping take? +
Standard shipping takes 3-5 business days. Express options are available at checkout.
Do you ship internationally? +
Yes, we ship to over 50 countries. Rates are calculated at checkout.
Accordion
Smooth FAQ accordion with plus/minus icons.