A before and after image comparison slider built with Tailwind CSS and a few lines of vanilla JavaScript. Two photos are stacked in one frame and a draggable divider reveals more of one or the other, which makes it a good fit for photo retouching portfolios, renovation projects, cleaning services, cosmetic results and product upgrades.
How it works
The root is figure.bas-compare. Inside .bas-frame, an aspect-video box, the after photo sits at the bottom and the before photo sits above it inside .bas-before. In style.css, .bas-before uses clip-path: inset(0 calc(100% - var(--bas-pos)) 0 0), so only the left part of the before photo is visible. The .bas-divider line and its round .bas-grip are placed with left: var(--bas-pos).
Interaction is handled by a native input type="range" (.bas-range) that covers the whole frame with opacity: 0. Because it is a real range input, mouse dragging, touch dragging, clicking anywhere in the image and the arrow, Page Up/Down, Home and End keys all work without custom pointer code. On every input event the script writes the new value into --bas-pos, updates aria-valuetext and the .bas-readout text. The three buttons marked data-bas-jump set the position to 100, 50 or 0; for these jumps the .bas-animate class adds a short transition so the divider glides instead of snapping. The pressed button is highlighted through aria-pressed and the aria-pressed: variant.
In this demo both layers use the same Unsplash landscape photo and .bas-before-img applies a CSS filter to imitate an unedited RAW file. Replace both src values with your own real before and after photos and delete the filter rule.
Customization
data-bas-starton.bas-compare: the starting position in percent, e.g.30to show mostly the after photo on load.--bas-speedon.bas-compareinstyle.css: duration of the glide used by the jump buttons.aspect-videoon.bas-frame: useaspect-squareoraspect-[4/3]to match portrait or product shots..bas-before-imgfilterinstyle.css: remove it when you use two different photos.bg-teal-400on the After label andaria-pressed:bg-teal-400on the buttons: switchtealto another color for your brand.step="1"on.bas-range: usestep="5"to make each arrow key press move the divider further.h-12 w-12on.bas-grip: make the handle bigger for touch-first layouts.
Use cases
- Photo retouching portfolios: visitors drag the divider to compare the original and the edited image
- Renovation and construction projects: a before and after view shows the transformation of a room or building
- Cleaning and detailing services: jump buttons switch quickly between the dirty and the finished result
- Beauty and cosmetic clinics: a keyboard-accessible slider presents treatment results in one frame
Accessibility
The range input has a visually hidden label and a readable aria-valuetext such as "50% before, 50% after", so screen readers announce the split. Both images have descriptive alt text, the Before and After badges are aria-hidden to avoid duplicate announcements, and the jump buttons sit in a labelled role="group". Keyboard focus is shown as a teal outline around the grip through :has(.bas-range:focus-visible). The glide transition is disabled under prefers-reduced-motion: reduce.
Usage notes
The photo comes from Unsplash; replace it with your own images. For several sliders on one page, give each range input and its label a unique id.