Skip to content

Before and After Image Comparison Slider

Tailwind CSS before and after image slider with a draggable divider, keyboard support through a native range input and quick jump buttons.

Also known as: image comparison slider, before after photo slider, before and after slider, photo reveal slider

Tailwind CSS Galleries Responsive Dark #comparison #gallery #image #responsive #slider

Before and After Image Comparison Slider live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

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-start on .bas-compare: the starting position in percent, e.g. 30 to show mostly the after photo on load.
  • --bas-speed on .bas-compare in style.css: duration of the glide used by the jump buttons.
  • aspect-video on .bas-frame: use aspect-square or aspect-[4/3] to match portrait or product shots.
  • .bas-before-img filter in style.css: remove it when you use two different photos.
  • bg-teal-400 on the After label and aria-pressed:bg-teal-400 on the buttons: switch teal to another color for your brand.
  • step="1" on .bas-range: use step="5" to make each arrow key press move the divider further.
  • h-12 w-12 on .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.

Before and After Image Comparison Slider at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile, dark design
Category
Galleries
License
Free for personal and commercial use

Questions about this template

Can I use the Before and After Image Comparison Slider code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Before and After Image Comparison Slider – free Tailwind CSS gallery template
Before and After Image Comparison Slider · free Tailwind CSS gallery template

More Galleries

CSS Grid Bento Photo Gallery – free Pure CSS gallery template

CSS Grid Bento Photo Gallery

Bento-style photo gallery with mixed tile sizes from CSS Grid spans, hover captions and a keyboard-friendly dialog lightbox.

Polaroid Photo Gallery – free Pure CSS gallery template

Polaroid Photo Gallery

Pure CSS polaroid gallery on a cork board: tilted photo frames with tape and handwritten captions that straighten and lift on hover or focus.

Masonry Image Gallery – free Pure CSS gallery template

Masonry Image Gallery

Pure CSS masonry image gallery using multi-column layout, with hover and focus captions that slide up over each photo. No JavaScript needed.

Justified Photo Gallery Rows – free Tailwind CSS gallery template

Justified Photo Gallery Rows

Flickr-style Tailwind CSS gallery where photos of any ratio fill each row at equal heights, with a live row-height slider.

Before and After Image Slider for Shopify – free Shopify gallery template

Before and After Image Slider for Shopify

Drag-to-compare before and after image slider for Shopify with vertical or horizontal divider, touch and keyboard control, and optional side text.