{"id":146,"date":"2026-10-18T21:10:27","date_gmt":"2026-10-18T21:10:27","guid":{"rendered":"https:\/\/refracttools.com\/blog\/?p=146"},"modified":"2026-10-18T21:10:27","modified_gmt":"2026-10-18T21:10:27","slug":"before-after-image-slider","status":"publish","type":"post","link":"https:\/\/refracttools.com\/blog\/ui-design\/before-after-image-slider\/","title":{"rendered":"How to build an accessible before and after image slider"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The simplest accessible before and after slider stacks the two images in the same grid cell, trims the top image with <code>clip-path: inset()<\/code> controlled by a CSS custom property, and puts a full-size <code>&lt;input type=\"range\"&gt;<\/code> over both. Dragging the range updates the property, so a three-line script is all the JavaScript you need, and the slider works with a mouse, touch and keyboard out of the box. The <a href=\"https:\/\/refracttools.com\/before-after-slider\/\">before and after slider builder<\/a> generates this code with your own images.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-refract-takeaways is-layout-constrained wp-block-group-is-layout-constrained\">\n<h3 class=\"wp-block-heading\">Key takeaways<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stack both images with CSS grid; clip the &#8220;after&#8221; image with <code>clip-path: inset(0 0 0 var(--pos))<\/code>.<\/li>\n\n\n<li>A real range input gives you keyboard, touch and screen reader support without a library.<\/li>\n\n\n<li>Both images need alt text, and the two must have identical dimensions and framing.<\/li>\n\n\n<li>Give the handle a visible focus style so keyboard users can see where they are.<\/li>\n<\/ul>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How does a before and after slider work?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both images occupy the same space, one on top of the other. The top (&#8220;after&#8221;) image is clipped from the left by a percentage. At 50%, you see the left half of the before image and the right half of the after image. Moving the slider changes that percentage, revealing more of one or the other. A thin line and a handle drawn at the same position make the split obvious.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The HTML, CSS and JavaScript<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"ba\" style=\"--pos: 50%\"&gt;\n  &lt;img src=\"before.jpg\" alt=\"Homepage before the redesign\" width=\"1200\" height=\"800\"&gt;\n  &lt;img class=\"ba__after\" src=\"after.jpg\" alt=\"Homepage after the redesign\" width=\"1200\" height=\"800\"&gt;\n  &lt;input class=\"ba__range\" type=\"range\" min=\"0\" max=\"100\" value=\"50\"\n         aria-label=\"Compare before and after\"&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.ba { display: grid; position: relative; }\n.ba &gt; * { grid-area: 1 \/ 1; }\n.ba img { display: block; width: 100%; height: auto; }\n\n.ba__after { clip-path: inset(0 0 0 var(--pos)); }\n\n.ba::after {                 \/* the divider line *\/\n  content: \"\";\n  position: absolute;\n  inset: 0 auto 0 var(--pos);\n  width: 2px;\n  background: #fff;\n  pointer-events: none;\n}\n\n.ba__range {\n  appearance: none;\n  margin: 0;\n  background: none;\n  opacity: 0;               \/* invisible, but still the control *\/\n  cursor: ew-resize;\n}\n\n.ba:has(.ba__range:focus-visible) {\n  outline: 3px solid #2f8cff;\n  outline-offset: 4px;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>document.querySelectorAll('.ba__range').forEach(range =&gt; {\n  range.addEventListener('input', () =&gt;\n    range.parentElement.style.setProperty('--pos', range.value + '%'));\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The range input fills the whole slider, so clicking or dragging anywhere moves the split, and arrow keys move it in steps once it has focus. The script attaches to every slider with the same class, so several on one page all work.<\/p>\n\n\n<aside class=\"tool-card tool-card--inline\"><span class=\"swatch\" style=\"background:linear-gradient(90deg,#8a8a8a 0 50%,#f59e0b 50%)\" aria-hidden=\"true\"><\/span><div class=\"tool-card__body\"><p class=\"tool-card__eyebrow\">Free tool<\/p><p class=\"tool-card__name\">Before and after slider builder<\/p><p class=\"tool-card__blurb\">Accessible before-and-after comparison sliders to embed anywhere.<\/p><\/div><a class=\"btn primary\" href=\"https:\/\/refracttools.com\/before-after-slider\/\">Try it free <span aria-hidden=\"true\">\u2192<\/span><\/a><\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">Why use a range input instead of a custom drag handle?<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><div class=\"table-scroll\" tabindex=\"0\" role=\"region\" aria-label=\"Scrollable table\"><table><thead><tr><th>Behaviour<\/th><th>Range input<\/th><th>Custom div handle<\/th><\/tr><\/thead><tbody><tr><td>Mouse and touch<\/td><td>Built in<\/td><td>Needs pointer event code<\/td><\/tr><tr><td>Keyboard<\/td><td>Arrow keys, Home and End built in<\/td><td>Needs key handlers<\/td><\/tr><tr><td>Screen readers<\/td><td>Announced as a slider with its value<\/td><td>Needs ARIA roles and value updates<\/td><\/tr><tr><td>Focus<\/td><td>Focusable by default<\/td><td>Needs tabindex<\/td><\/tr><tr><td>Code size<\/td><td>Three lines of script<\/td><td>Often a library<\/td><\/tr><\/tbody><\/table><\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Native controls carry years of browser accessibility work for free. Rebuilding them with divs usually leaves something out, most often keyboard support.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-refract-callout is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\"><strong>Focus:<\/strong> Because the range input is invisible, the browser&#8217;s own focus ring is invisible too. The <code>:has()<\/code> rule above draws an outline around the whole slider when the input has keyboard focus. Keep something like it, so keyboard users can see the control.<\/p>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How do you make a top and bottom split?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Clip from the top instead of the left, and turn the range input vertical. Current browsers support vertical range inputs through <code>writing-mode<\/code>, so it still works with the arrow keys. Move the divider line to <code>top: var(--pos)<\/code> and make it 2px tall and full width.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.ba--vertical .ba__after { clip-path: inset(var(--pos) 0 0 0); }\n.ba--vertical .ba__range { writing-mode: vertical-lr; cursor: ns-resize; }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Making both images line up<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The comparison only works if the two images have exactly the same dimensions, crop and alignment. If one is even a few pixels off, details jump as you drag and the effect looks broken.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Redesigns:<\/strong> take both screenshots at the same browser width and zoom level. The <a href=\"https:\/\/refracttools.com\/screenshot-mockup-generator\/\">screenshot mockup generator<\/a> can frame them consistently.<\/li>\n\n\n<li><strong>Photo edits:<\/strong> export both from the same file at the same size.<\/li>\n\n\n<li><strong>Compression tests:<\/strong> show the original and the compressed file side by side to prove there is no visible difference; see our guide to <a href=\"\/blog\/image-optimisation\/compress-images-for-website\/\">compressing images<\/a>.<\/li>\n\n\n<li><strong>Resize both together<\/strong> with the <a href=\"https:\/\/refracttools.com\/bulk-image-resizer\/\">bulk image resizer<\/a> if they come from different sources.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What are good uses for a before and after slider?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">They suit any change that is easiest to understand by looking: a website redesign, photo retouching, colour grading, image compression quality, renovation and landscaping projects, cleaning and restoration work, or a dark theme against a light one. Keep the before image honest. A slider that compares a deliberately poor &#8220;before&#8221; with a polished &#8220;after&#8221; is quickly spotted by visitors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Does a slider hurt page speed?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each slider loads two full images, so size them properly and lazy-load sliders that sit below the fold with <code>loading=\"lazy\"<\/code>. Include <code>width<\/code> and <code>height<\/code> attributes so the browser reserves space and nothing shifts while they load. Beyond that, the code is tiny: a few lines of CSS and three of JavaScript, with no dependencies.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Build one with your own images<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Drop your two images into the <a href=\"https:\/\/refracttools.com\/before-after-slider\/\">before and after slider builder<\/a>, choose a left\u2013right or top\u2013bottom split, set the labels and handle colour, and copy the code, or download a standalone demo page. Your images stay on your device while you preview.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Last checked: October 2026.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Sources: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input\/range\" rel=\"noopener\" target=\"_blank\">MDN: &lt;input type=&#8221;range&#8221;&gt;<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/basic-shape\/inset\" rel=\"noopener\" target=\"_blank\">MDN: inset()<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/:has\" rel=\"noopener\" target=\"_blank\">MDN: :has()<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A before and after image slider needs no library: two stacked images, clip-path: inset() and a native range input. Here is the full code, how to make it accessible, and how to line the images up.<\/p>\n","protected":false},"author":1,"featured_media":145,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_refract_tool":"before-after-slider","footnotes":""},"categories":[24],"tags":[20,5,13],"class_list":["post-146","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ui-design","tag-accessibility","tag-image-tools","tag-ui-design"],"_links":{"self":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":1,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions\/165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/media\/145"}],"wp:attachment":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}