Image compare
A before and after pair of pictures with a draggable divider.
Example
<focsd-image-compare label="Bridge, 1990 and today" style="max-inline-size: 28rem"> <img slot="before" src="/media/bridge-1990.svg" alt="The bridge in 1990, brown steel" width="800" height="450" /> <img slot="after" src="/media/bridge-today.svg" alt="The bridge today, painted red" width="800" height="450" /></focsd-image-compare>Vertical
A starting position and a vertical divider.
<focsd-image-compare orientation="vertical" position="30" label="Bridge, vertical" style="max-inline-size: 20rem"> <img slot="before" src="/media/bridge-1990.svg" alt="The bridge in 1990" width="800" height="450" /> <img slot="after" src="/media/bridge-today.svg" alt="The bridge today" width="800" height="450" /></focsd-image-compare>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-image-compare.js';</script>
<focsd-image-compare label="Bridge, 1990 and today">…</focsd-image-compare>import { ImageCompare } from '@focsd-dev/react';
export function Example() { return <ImageCompare label="Bridge, 1990 and today">…</ImageCompare>;}<script setup>import '@focsd-dev/components/focsd-image-compare.js';</script>
<template> <focsd-image-compare label="Bridge, 1990 and today">…</focsd-image-compare></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-image-compare.js';
// template<focsd-image-compare label="Bridge, 1990 and today">…</focsd-image-compare><script> import '@focsd-dev/components/focsd-image-compare.js';</script>
<focsd-image-compare label="Bridge, 1990 and today">…</focsd-image-compare>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
position | position | number | 50 |
label | label | string | — |
orientation | orientation | 'horizontal' | 'vertical' | horizontal |
Slots
| Slot | Description |
|---|---|
before | The before image (with alt), drawn on the inline-start (or top) side. |
after | The after image (with alt). |
CSS parts
Style the inside of the component with ::part(), for example focsd-image-compare::part(figure).
| Part | Description |
|---|---|
figure | The figure that holds both images. |
before | The clipped wrapper of the before image. |
after | The wrapper of the after image. |
divider | The line between the images. |
handle | The slider thumb on the divider. |
Accessibility
- The divider handle is a slider (
role="slider") from 0 to 100, with the value said in words: "60% before image". Both pictures'alttexts are in its description, so a screen reader hears what each side shows. - The before image sits on the inline-start side (left in left-to-right text, right in right-to-left), and the slider value is how much of the before image shows.
- Dragging works anywhere on the figure, not only on the handle, and a drag moves focus to the handle.
focsd-position-changefires once at the end of a drag and on each key press. - Give both pictures real
alttext. A missingaltis warned about once, because the slider has nothing to describe without it. - With
orientation="vertical"the divider is horizontal and the before image is on top.
Keyboard
| Key | Action |
|---|---|
| Arrow keys | Move the divider by 1 (Left and Right swap in right-to-left text) |
| Shift + arrow / Page Up / Page Down | Move the divider by 10 |
| Home / End | Show all of the after image / all of the before image |
Vertical
- Up and Right arrows raise the value and Down and Left lower it, as for any slider (APG), so the divider moves down when you press Up. The value is how much of the before image shows.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.