Before After Slider: HTML, CSS and JS Code

A before-after slider is two images stacked on top of each other, with a divider the visitor drags to reveal more of one or the other. You can build it with a range input, a CSS clip-path and four lines of JavaScript, or add it to any site with a single HTML attribute. Both ways are below.
What is a before-after slider?
A before-after slider (also called an image comparison slider) lets visitors drag a divider between two images to see the difference. The first image sits behind the second, and the divider reveals more or less of each side as you drag.
Common use cases:
- Photo retouching and editing portfolios
- Home renovation and interior design showcases
- Product comparisons
- Website redesign presentations
- Dental, cosmetic, or fitness transformations
Instead of placing two images side by side and hoping visitors spot the difference, a slider makes the comparison interactive. Users control what they see, which keeps them on the page longer.
How do you build a before-after slider with HTML, CSS and JavaScript?
The idea: the "after" image sits underneath, the "before" image sits on top and is cut off at the divider with clip-path, and an invisible range input on top moves the divider.
<div class="ba">
<img src="after.jpg" alt="After">
<img class="ba-before" src="before.jpg" alt="Before">
<input type="range" min="0" max="100" value="50" aria-label="Compare before and after">
</div>
.ba { --pos: 50%; position: relative; aspect-ratio: 16 / 9; overflow: hidden; }
.ba img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.ba-before { clip-path: inset(0 calc(100% - var(--pos)) 0 0); }
.ba::after { content: ""; position: absolute; top: 0; bottom: 0; left: var(--pos); width: 2px; background: #fff; pointer-events: none; }
.ba input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: ew-resize; }
.ba:focus-within { outline: 2px solid; }
document.querySelectorAll('.ba').forEach(ba => {
ba.querySelector('input').addEventListener('input', e => {
ba.style.setProperty('--pos', e.target.value + '%');
});
});
Because the control is a native range input, dragging works with a mouse and with touch, and the arrow keys move the divider, so it stays accessible. Both images must have the same proportions, or object-fit: cover will crop one of them.
How do you add a before-after slider with one HTML attribute?
If you do not want to maintain the code yourself, the OneType Transforms script does the same job. First add the script tag to your site:
<script src="https://cdn.transforms.onetype.ai/script/57095981335841558d328d599e6f5322.js"></script>
Then put two images in a div with the ot="before-after" attribute:
<div ot="before-after">
<img src="before.jpg" alt="Before">
<img src="after.jpg" alt="After">
</div>
The first image becomes the "before" side and the second the "after" side. The handle, the divider and the drag behaviour appear automatically.
Can you customize the before-after slider?
Yes. Everything is controlled through HTML attributes - just add them to the same div. Here are all the options:
Starting position - control where the slider starts. Default is 50 (middle). Set it to any value between 0 and 100:
<div ot="before-after" ot-position="30">
Vertical mode - slide top to bottom instead of left to right. Useful for landscape images or when the change happens vertically:
<div ot="before-after" ot-direction="vertical">
Labels - add text labels to each side so visitors know which is which:
<div ot="before-after" ot-label-before="Original" ot-label-after="Edited">
Hover mode - instead of dragging, the slider follows the mouse cursor. Good for portfolios where you want instant interaction without clicking:
<div ot="before-after" ot-hover="true">
You can combine all of these together:
<div ot="before-after" ot-direction="vertical" ot-label-before="Before" ot-label-after="After" ot-position="30" ot-hover="true">
<img src="before.jpg" alt="Before">
<img src="after.jpg" alt="After">
</div>
Does it work on Webflow, WordPress, and Shopify?
Yes. It works on any website that lets you add custom HTML. Here is a quick overview for the most popular platforms:
Webflow
Paste the script tag in Site Settings > Custom Code > Footer Code. Then add an Embed element to your page and paste the before-after HTML. Requires a Basic plan or higher. See the Webflow guide.
WordPress
Install the free WPCode plugin and paste the script tag in Header & Footer > Header. Then use a Custom HTML block in the editor for the slider. Works on any self-hosted WordPress site. See the WordPress guide.
Squarespace
Add the script tag in Website > Website Tools > Code Injection > Header. Use a Code Block (set to HTML, Display Source off) for the slider markup. Requires a Core plan or higher. See the Squarespace guide.
Shopify
Add the script tag in theme.liquid before . Then use a Custom Liquid section in the theme editor for the slider HTML. Works on Basic plan and up. See the Shopify guide.
Framer
Add the script tag in Settings > General > Custom Code > End of head tag. Use an Embed component for the slider HTML. Requires a Basic plan or higher. See the Framer guide.
The same approach works on Elementor, Wix, static HTML sites, or any platform where you can add custom code.
Is the before-after slider free?
Yes. The before-after slider is completely free. No account needed, no token, no restrictions. Just add the script tag and use it.
It is part of OneType Transforms - a collection of drop-in interactive components for websites. Some transforms are free (like this one), and there are premium ones too.
Does the slider work on mobile?
Yes. It uses a native range input under the hood, so touch dragging on phones and tablets works out of the box. It also supports keyboard navigation and screen readers for accessibility.
The images scale to fit their container, so the slider is responsive by default. It looks the same on a 4K monitor and a phone screen.
Building the whole website, not one effect?
Describe your business in one sentence and Divhunt Studio builds the site for you. You can start from one of the website templates and change anything by chatting. Building is free; you pay when you publish.
FAQ
How do I make a before and after slider in JavaScript?
Stack two images, clip the top one with clip-path: inset(), and update a CSS variable from a range input, as in the code above.
Can I make a vertical before-after slider?
With the attribute version, use ot-direction="vertical". The plain version above is horizontal only; a vertical slider needs a rotated range input and a second clip-path, so the attribute is the simpler route.
Why do my images look misaligned in the slider?
The two images have different proportions. Export both at the same size, or set the same aspect-ratio on the container.
