WebCraft

clip-path: inset()

Before and after images with CSS clip-path

Two images that keep the same crop

Live demo

Restoration preview
Faded workshop before restoration Workshop with restored blue doors

Caveat and fallback

CaveatBoth images must keep their full box while the upper one is clipped. Changing its width would resize its contents and make the facade drift away from the base image. Grid stacks the pair in the same cell; clip-path: inset() reveals a percentage without changing layout. The radio group gives three keyboard-operable stops rather than pretending to be a continuous slider. Switch from 25% to 75% and watch the door edges stay aligned. The two original SVG illustrations are included in the demo; replace both image URLs with your own registered pair.

FallbackWithout :has(), this remains a static 50/50 comparison. Without clip-path, the upper image covers the lower one, so provide separate originals if both versions must always be inspectable. Use matching aspect ratios and matching object-position for real photographs; CSS cannot register two shots taken from different viewpoints. For a continuous drag handle, add a native range input and a small script instead of an inaccessible draggable div. Reference: MDN documentation.

Source code

HTML + CSS
<fieldset class="d-compare">
  <legend>Restoration preview</legend>
  <div class="d-compare-controls">
    <label><input type="radio" name="wc-reveal"
      value="25">25% restored</label>
    <label><input type="radio" name="wc-reveal"
      value="50" checked>50%</label>
    <label><input type="radio" name="wc-reveal"
      value="75">75%</label>
  </div>
  <div class="d-compare-frame">
    <img src="/assets/restoration-before.svg"
      width="600" height="360"
      alt="Faded workshop before restoration">
    <img src="/assets/restoration-after.svg"
      width="600" height="360"
      alt="Workshop with restored blue doors">
  </div>
</fieldset>

.d-compare {
  --cut: 50%; border: 0; padding: 0; margin: 0;
  width: 100%; max-width: 26rem;
  color: var(--paper, #e6eaf1);
}
.d-compare legend { margin-bottom: .75rem; }
.d-compare-controls {
  display: flex; flex-wrap: wrap; gap: .8rem;
  margin-bottom: .75rem; font-size: .75rem;
}
.d-compare input { accent-color: #6fa8dc; }
.d-compare:has([value="25"]:checked) {
  --cut: 75%;
}
.d-compare:has([value="75"]:checked) {
  --cut: 25%;
}
.d-compare-frame { display: grid; }
.d-compare img {
  grid-area: 1 / 1; width: 100%; height: auto;
  aspect-ratio: 5 / 3; object-fit: cover;
}
.d-compare img + img {
  clip-path: inset(0 var(--cut) 0 0);
}
.d-compare-frame::after {
  content: ""; grid-area: 1 / 1;
  z-index: 1; pointer-events: none;
  width: 2px; background: white;
  margin-left: calc(100% - var(--cut));
}
.d-compare label:has(:focus-visible) {
  outline: 2px solid currentColor;
  outline-offset: 3px;
}

Published

Questions

<details name>

Why do my before and after images stop lining up?

Reducing the upper image's width resizes its contents. Keep both images in the same full-size grid cell and reveal part of the upper image with clip-path. Both source images still need matching viewpoints and framing.

Can a before and after comparison work without JavaScript?

Radio inputs can select a few fixed reveal positions, with :has() passing the chosen position to the clipping rule. This example offers three stops. A continuously adjustable range input needs a script to pass its value to the CSS.

Does clipping an image change its layout size?

clip-path changes the visible region without changing the image's layout box. Keeping that box fixed prevents the resizing drift that occurs when the reveal is implemented by changing the image width.