Skip to the main content

Before and After Compare Slider

Free Media

Two stacked local SVG pictures with a range input that moves a divider between them, and the split read out as a percentage.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="bac" aria-labelledby="bac-title">
  <h2 class="bac__title" id="bac-title">Before and after</h2>
  <figure class="bac__figure">
    <div class="bac__stage">
      <img class="bac__img" src="assets/plot-before.svg" width="800" height="450" alt="A garden plot before work: bare soil in rows, string lines on stakes and a plain grey shed.">
      <img class="bac__img bac__img--after" src="assets/plot-after.svg" width="800" height="450" alt="The same plot after work: green plants in every row, flowers by a painted shed and a bright sky.">
      <span class="bac__tag bac__tag--before" aria-hidden="true">Before</span>
      <span class="bac__tag bac__tag--after" aria-hidden="true">After</span>
      <span class="bac__divider" aria-hidden="true"><span class="bac__grip"></span></span>
      <input class="bac__range" id="bac-range" type="range" min="0" max="100" step="1" value="50" aria-label="Move the divider between before and after" aria-valuetext="Before 50 percent, after 50 percent" hidden>
    </div>
    <figcaption class="bac__caption">
      <span>An example drawing. Left is before, right is after.</span>
      <output class="bac__out" for="bac-range" id="bac-out">Before 50%, after 50%</output>
    </figcaption>
  </figure>
</section>

CSS

.bac {
  --bac-bg: #ffffff;
  --bac-tint: #f3f4fb;
  --bac-ink: #1c2033;
  --bac-muted: #4b5266;
  --bac-line: #d5d9e6;
  --bac-accent: #4338ca;
  --bac-focus: #1c2033;
  --bac-ring: #ffffff;
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  max-width: 36rem;
  padding: 1.25rem;
  color: var(--bac-ink);
  background: var(--bac-bg);
  border: 1px solid var(--bac-line);
  border-radius: 16px;
  box-shadow: 0 1px 2px rgba(28, 32, 51, 0.06), 0 10px 28px -14px rgba(28, 32, 51, 0.22);
}
.bac *, .bac *::before, .bac *::after { box-sizing: inherit; }
.bac [hidden] { display: none; }
.bac__title { margin: 0 0 0.7rem; font-size: 1.3rem; line-height: 1.2; letter-spacing: -0.01em; }
.bac__figure { margin: 0; }

.bac__stage { --bac-v: 0.5; position: relative; aspect-ratio: 2 / 1; overflow: hidden; background: var(--bac-tint); border-radius: 12px; }
.bac__img { position: absolute; inset: 0; display: block; width: 100%; height: 100%; object-fit: cover; user-select: none; -webkit-user-drag: none; }
.bac__img--after { clip-path: inset(0 0 0 calc(22px + (100% - 44px) * var(--bac-v))); }

.bac__tag {
  position: absolute; top: 0.6rem; padding: 0.2rem 0.65rem;
  font-size: 0.78rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  color: #ffffff; background: #1c2033; border-radius: 999px;
}
.bac__tag--before { left: 0.6rem; }
.bac__tag--after { right: 0.6rem; }

.bac__divider {
  position: absolute; top: 0; bottom: 0; left: calc(22px + (100% - 44px) * var(--bac-v));
  width: 3px; margin-left: -1.5px; background: #ffffff; box-shadow: 0 0 0 1px rgba(28, 32, 51, 0.5); pointer-events: none;
}
.bac__grip {
  position: absolute; top: 50%; left: 50%; width: 2.75rem; height: 2.75rem; margin: -1.375rem 0 0 -1.375rem;
  background: #ffffff; border: 2px solid #1c2033; border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}
.bac__grip::before, .bac__grip::after { content: ""; position: absolute; top: 50%; width: 0.5rem; height: 0.5rem; margin-top: -0.25rem; border-top: 2.5px solid #1c2033; border-left: 2.5px solid #1c2033; }
.bac__grip::before { left: 0.6rem; transform: rotate(-45deg); }
.bac__grip::after { right: 0.6rem; transform: rotate(135deg); }

.bac__range {
  position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; padding: 0;
  background: transparent; cursor: ew-resize; appearance: none; -webkit-appearance: none;
}
.bac__range::-webkit-slider-runnable-track { height: 100%; background: transparent; }
.bac__range::-moz-range-track { height: 100%; background: transparent; }
.bac__range::-webkit-slider-thumb { width: 44px; height: 100%; background: transparent; border: 0; appearance: none; -webkit-appearance: none; }
.bac__range::-moz-range-thumb { width: 44px; height: 100%; background: transparent; border: 0; }
.bac__range:focus-visible { outline: 3px solid var(--bac-focus); outline-offset: -6px; box-shadow: inset 0 0 0 9px var(--bac-ring); }

.bac__caption { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 0.25rem 1rem; margin-top: 0.75rem; color: var(--bac-muted); font-size: 0.85rem; }
.bac__out { color: var(--bac-ink); font-weight: 650; font-variant-numeric: tabular-nums; }

@media (max-width: 30rem) {
  .bac { padding: 1rem; }
  .bac__tag { top: 0.45rem; font-size: 0.7rem; }
}
@media (prefers-color-scheme: dark) {
  .bac {
    --bac-bg: #1c2033; --bac-tint: #272c45; --bac-ink: #eceef8; --bac-muted: #b3b9d0; --bac-line: #3d4466;
    --bac-accent: #b4b9ff; --bac-focus: #ffffff; --bac-ring: #1c2033;
    box-shadow: none;
  }
}

JavaScript

(function () {
  'use strict';

  var stage = document.querySelector('.bac__stage');
  var range = document.getElementById('bac-range');
  var out = document.getElementById('bac-out');
  if (!stage || !range || !out) return;

  // The divider is a CSS custom property (0 to 1) that the styles read.
  function move() {
    var value = Number(range.value);
    stage.style.setProperty('--bac-v', String(value / 100));
    out.textContent = 'Before ' + value + '%, after ' + (100 - value) + '%';
    range.setAttribute('aria-valuetext', 'Before ' + value + ' percent, after ' + (100 - value) + ' percent');
  }

  range.hidden = false;
  range.addEventListener('input', move);
  move();
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • The control is a native range input, so arrow keys, Home and End all work.
  • Its value is spoken as before and after percentages, and written beside the picture.
  • Both pictures have a text alternative that describes what changed.