Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Image Compare

    Before and after view with a divider you drag, click, or move with the keyboard. Horizontal or vertical, and it takes any content, including img.

    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    @php
        $frame = 'relative aspect-[8/5] w-full overflow-hidden bg-linear-to-b from-warning/35 via-warning/10 to-success/20';
        $sun = 'absolute top-8 right-20 size-14 rounded-full bg-warning';
        $hills = 'absolute inset-x-0 bottom-0 h-3/5 w-full';
        $far = 'fill-success/50';
        $near = 'fill-success';
        $lake = 'fill-surface/50';
    
        // One deterministic landscape, toned flat (before) or vivid (after).
        $panels = ['before' => 'grayscale contrast-75', 'after' => 'saturate-150'];
    @endphp
    
    <x-markas.image-compare aria-label="Photo edit, before and after">
        @foreach ($panels as $side => $tone)
            <x-slot :name="$side">
                <div class="{{ $frame }} {{ $tone }}">
                    <span class="{{ $sun }}"></span>
                    <svg class="{{ $hills }}" viewBox="0 0 480 180" preserveAspectRatio="none" aria-hidden="true">
                        <path class="{{ $far }}" d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z" />
                        <path class="{{ $near }}" d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z" />
                        <path class="{{ $lake }}" d="M0 160c120-12 360-12 480 0v20H0Z" />
                    </svg>
                </div>
            </x-slot>
        @endforeach
    </x-markas.image-compare>
    import { ImageCompare } from "@/components/markas/ImageCompare";
    
    const frame =
      "relative aspect-[8/5] w-full overflow-hidden bg-linear-to-b from-warning/35 via-warning/10 to-success/20";
    const sun = "absolute top-8 right-20 size-14 rounded-full bg-warning";
    const hills = "absolute inset-x-0 bottom-0 h-3/5 w-full";
    const far = "fill-success/50";
    const near = "fill-success";
    const lake = "fill-surface/50";
    
    /** One deterministic landscape, toned flat (before) or vivid (after). */
    const scene = (tone: string) => (
      <div className={`${frame} ${tone}`}>
        <span className={sun}></span>
        <svg
          className={hills}
          viewBox="0 0 480 180"
          preserveAspectRatio="none"
          aria-hidden="true"
        >
          <path
            className={far}
            d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z"
          />
          <path
            className={near}
            d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z"
          />
          <path className={lake} d="M0 160c120-12 360-12 480 0v20H0Z" />
        </svg>
      </div>
    );
    
    export default function ImageCompareExample() {
      return (
        <ImageCompare
          before={scene("grayscale contrast-75")}
          after={scene("saturate-150")}
          aria-label="Photo edit, before and after"
        />
      );
    }
    <script setup lang="ts">
    import ImageCompare from "@/components/markas/ImageCompare.vue";
    
    const frame =
      "relative aspect-[8/5] w-full overflow-hidden bg-linear-to-b from-warning/35 via-warning/10 to-success/20";
    const sun = "absolute top-8 right-20 size-14 rounded-full bg-warning";
    const hills = "absolute inset-x-0 bottom-0 h-3/5 w-full";
    const far = "fill-success/50";
    const near = "fill-success";
    const lake = "fill-surface/50";
    
    /** One deterministic landscape, toned flat (before) or vivid (after). */
    const panels = [
      { slot: "before", tone: "grayscale contrast-75" },
      { slot: "after", tone: "saturate-150" },
    ] as const;
    </script>
    
    <template>
      <ImageCompare aria-label="Photo edit, before and after">
        <template v-for="panel in panels" :key="panel.slot" #[panel.slot]>
          <div :class="`${frame} ${panel.tone}`">
            <span :class="sun"></span>
            <svg
              :class="hills"
              viewBox="0 0 480 180"
              preserveAspectRatio="none"
              aria-hidden="true"
            >
              <path
                :class="far"
                d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z"
              />
              <path
                :class="near"
                d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z"
              />
              <path :class="lake" d="M0 160c120-12 360-12 480 0v20H0Z" />
            </svg>
          </div>
        </template>
      </ImageCompare>
    </template>
    <script lang="ts">
      import ImageCompare from "$lib/components/markas/ImageCompare.svelte";
    
      const frame =
        "relative aspect-[8/5] w-full overflow-hidden bg-linear-to-b from-warning/35 via-warning/10 to-success/20";
      const sun = "absolute top-8 right-20 size-14 rounded-full bg-warning";
      const hills = "absolute inset-x-0 bottom-0 h-3/5 w-full";
      const far = "fill-success/50";
      const near = "fill-success";
      const lake = "fill-surface/50";
    </script>
    
    <!-- One deterministic landscape, toned flat (before) or vivid (after). -->
    {#snippet scene(tone: string)}
      <div class={`${frame} ${tone}`}>
        <span class={sun}></span>
        <svg
          class={hills}
          viewBox="0 0 480 180"
          preserveAspectRatio="none"
          aria-hidden="true"
        >
          <path class={far} d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z" />
          <path class={near} d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z" />
          <path class={lake} d="M0 160c120-12 360-12 480 0v20H0Z" />
        </svg>
      </div>
    {/snippet}
    
    <ImageCompare aria-label="Photo edit, before and after">
      {#snippet before()}{@render scene("grayscale contrast-75")}{/snippet}
      {#snippet after()}{@render scene("saturate-150")}{/snippet}
    </ImageCompare>
    ---
    import ImageCompare from "@/components/markas/ImageCompare.astro";
    
    const frame =
      "relative aspect-[8/5] w-full overflow-hidden bg-linear-to-b from-warning/35 via-warning/10 to-success/20";
    const sun = "absolute top-8 right-20 size-14 rounded-full bg-warning";
    const hills = "absolute inset-x-0 bottom-0 h-3/5 w-full";
    const far = "fill-success/50";
    const near = "fill-success";
    const lake = "fill-surface/50";
    /** One deterministic landscape, toned flat (before) or vivid (after). */
    const panels = [
      { slot: "before", tone: "grayscale contrast-75" },
      { slot: "after", tone: "saturate-150" },
    ];
    ---
    
    <ImageCompare aria-label="Photo edit, before and after">
      {
        panels.map((panel) => (
          <div slot={panel.slot} class={`${frame} ${panel.tone}`}>
            <span class={sun}></span>
            <svg
              class={hills}
              viewBox="0 0 480 180"
              preserveAspectRatio="none"
              aria-hidden="true"
            >
              <path class={far} d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z"></path>
              <path class={near} d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z"></path>
              <path class={lake} d="M0 160c120-12 360-12 480 0v20H0Z"></path>
            </svg>
          </div>
        ))
      }
    </ImageCompare>
    import { Component } from "@angular/core";
    import { ImageCompare } from "./markas/image-compare.component";
    
    @Component({
      selector: "markas-example",
      imports: [ImageCompare],
      template: `
        <!-- One deterministic landscape, toned flat (before) or vivid (after). -->
        <markas-image-compare aria-label="Photo edit, before and after">
          <div before [class]="frame + ' contrast-75 grayscale'">
            <span [class]="sun"></span>
            <svg
              [class]="hills"
              viewBox="0 0 480 180"
              preserveAspectRatio="none"
              aria-hidden="true"
            >
              <path
                [class]="far"
                d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z"
              />
              <path
                [class]="near"
                d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z"
              />
              <path [class]="lake" d="M0 160c120-12 360-12 480 0v20H0Z" />
            </svg>
          </div>
          <div after [class]="frame + ' saturate-150'">
            <span [class]="sun"></span>
            <svg
              [class]="hills"
              viewBox="0 0 480 180"
              preserveAspectRatio="none"
              aria-hidden="true"
            >
              <path
                [class]="far"
                d="M0 90 70 40l70 45 80-55 90 65 80-50 90 35v100H0Z"
              />
              <path
                [class]="near"
                d="M0 130c80-30 150-10 230-25s170 15 250-5v80H0Z"
              />
              <path [class]="lake" d="M0 160c120-12 360-12 480 0v20H0Z" />
            </svg>
          </div>
        </markas-image-compare>
      `,
    })
    export default class ImageCompareExample {
      protected readonly frame =
        "relative aspect-[8/5] w-full overflow-hidden bg-linear-to-b from-warning/35 via-warning/10 to-success/20";
      protected readonly sun =
        "absolute top-8 right-20 size-14 rounded-full bg-warning";
      protected readonly hills = "absolute inset-x-0 bottom-0 h-3/5 w-full";
      protected readonly far = "fill-success/50";
      protected readonly near = "fill-success";
      protected readonly lake = "fill-surface/50";
    }

    Examples

    Redesign

    Revenue this month

    $48,210 +12%

    New

    Revenue: 48210 USD

    Old

    Revenue this month

    $48,210 +12%

    New

    Revenue: 48210 USD

    Old

    Revenue this month

    $48,210 +12%

    New

    Revenue: 48210 USD

    Old

    Revenue this month

    $48,210 +12%

    New

    Revenue: 48210 USD

    Old

    Revenue this month

    $48,210 +12%

    New

    Revenue: 48210 USD

    Old

    Vertical split

    Ceramic pour-over

    Hand glazed, holds 600 ml.

    $34

    Add to cart
    Final
    Wireframe

    Ceramic pour-over

    Hand glazed, holds 600 ml.

    $34

    Add to cart
    Final
    Wireframe

    Ceramic pour-over

    Hand glazed, holds 600 ml.

    $34

    Add to cart
    Final
    Wireframe

    Ceramic pour-over

    Hand glazed, holds 600 ml.

    $34

    Add to cart
    Final
    Wireframe

    Ceramic pour-over

    Hand glazed, holds 600 ml.

    $34

    Add to cart
    Final
    Wireframe

    Photo retouch with caption

    Calm
    Retouched
    Calm
    Original
    Drag the handle to compare the studio retouch.
    Calm
    Retouched
    Calm
    Original
    Drag the handle to compare the studio retouch.
    Calm
    Retouched
    Calm
    Original
    Drag the handle to compare the studio retouch.
    Calm
    Retouched
    Calm
    Original
    Drag the handle to compare the studio retouch.
    Calm
    Retouched
    Calm
    Original
    Drag the handle to compare the studio retouch.

    Installation

    npx markasui@latest add image-compare

    Usage

    <x-markas.image-compare aria-label="Living room renovation" :position="40">
        <x-slot:before>
            <img src="/img/before.jpg" alt="Living room before renovation" class="aspect-video size-full object-cover" />
        </x-slot:before>
        <x-slot:after>
            <img src="/img/after.jpg" alt="Living room after renovation" class="aspect-video size-full object-cover" />
        </x-slot:after>
    </x-markas.image-compare>

    Needs Alpine.js. The component dispatches position-change with the new percent.

    import { ImageCompare } from "@/components/markas/ImageCompare";
    
    <ImageCompare
      aria-label="Living room renovation"
      defaultPosition={40}
      before={<img src="/img/before.jpg" alt="Living room before renovation" />}
      after={<img src="/img/after.jpg" alt="Living room after renovation" />}
    />;
    <template>
      <ImageCompare v-model:position="position" aria-label="Living room renovation">
        <template #before
          ><img src="/img/before.jpg" alt="Living room before renovation"
        /></template>
        <template #after
          ><img src="/img/after.jpg" alt="Living room after renovation"
        /></template>
      </ImageCompare>
    </template>
    <script lang="ts">
      import ImageCompare from "$lib/components/markas/ImageCompare.svelte";
    
      let position = $state(50);
    </script>
    
    <ImageCompare bind:position aria-label="Kitchen renovation">
      {#snippet before()}<img src="/img/kitchen-old.webp" alt="" />{/snippet}
      {#snippet after()}<img src="/img/kitchen-new.webp" alt="" />{/snippet}
    </ImageCompare>
    ---
    import ImageCompare from "@/components/markas/ImageCompare.astro";
    ---
    
    <ImageCompare aria-label="Kitchen renovation">
      <img slot="before" src="/img/kitchen-old.webp" alt="" />
      <img slot="after" src="/img/kitchen-new.webp" alt="" />
    </ImageCompare>

    A small bundled script serves every comparison on the page and dispatches position-change.

    import { Component, signal } from "@angular/core";
    import { ImageCompare } from "./markas/image-compare.component";
    
    @Component({
      selector: "app-renovation",
      imports: [ImageCompare],
      template: `
        <markas-image-compare
          [(position)]="position"
          aria-label="Kitchen renovation"
        >
          <img before src="/img/kitchen-old.webp" alt="" />
          <img after src="/img/kitchen-new.webp" alt="" />
        </markas-image-compare>
      `,
    })
    export class Renovation {
      readonly position = signal(50);
    }
    • Before sits on the left (or top with orientation="vertical"), after on the right (or bottom). Give both the same size.
    • Drag anywhere, click a spot, or focus the handle and use the arrow keys, Page Up/Down, Home, and End.
    • Any content works: img, picture, svg, or plain markup such as an old and a new UI.
    • Attributes and class go to the outer element.

    Props

    NameTypeDefaultDescription
    before / afterReactNode / slot-The two views. Before sits on the start side (left or top), after on the end side. Both fill the same box, so give them the same size.
    orientation'horizontal' | 'vertical'horizontalHorizontal splits left and right with a vertical divider. Vertical splits top and bottom.
    position / defaultPosition / v-model:positionnumber50Starting divider position in percent from the start side. Blade uses position, React uses defaultPosition with onPositionChange, Vue uses v-model:position.
    stepnumber5Percent moved per arrow key press. Page Up and Page Down move 10.
    beforeLabelstringBeforeText of the chip on the before side.
    afterLabelstringAfterText of the chip on the after side.
    sliderLabelstringComparison positionAccessible name of the divider handle.
    positionChangeoutput<number>-Angular only. Emits the new position in percent; supports [(position)]. Svelte uses bind:position or onPositionChange, Astro dispatches position-change.

    Accessibility

    • The handle is a focusable role slider with aria-valuenow, aria-valuetext in percent, and aria-orientation.
    • Arrow keys move by step, Page Up and Page Down by 10, Home and End jump to either edge.
    • Pressing anywhere on the image moves the divider there and focuses the handle, so the keyboard can take over.
    • The divider glides to a clicked spot; with reduced motion it jumps there instead.

    Related components

    More markasui components with the same classes in every framework.