Skip to content
Focsd homeFocsd homeFocsd
Forms

Image compare

A before and after pair of pictures with a draggable divider.

  • Tag: focsd-image-compare
  • Pattern: Slider (window splitter)
  • Category: Display

Example

The bridge in 1990, brown steel The bridge today, painted red
HTML
<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.

The bridge in 1990 The bridge today
HTML
<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>

API

Attributes

AttributePropertyTypeDefault
positionpositionnumber50
labellabelstring—
orientationorientation'horizontal' | 'vertical'horizontal

Slots

SlotDescription
beforeThe before image (with alt), drawn on the inline-start (or top) side.
afterThe after image (with alt).

CSS parts

Style the inside of the component with ::part(), for example focsd-image-compare::part(figure).

PartDescription
figureThe figure that holds both images.
beforeThe clipped wrapper of the before image.
afterThe wrapper of the after image.
dividerThe line between the images.
handleThe 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' alt texts 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-change fires once at the end of a drag and on each key press.
  • Give both pictures real alt text. A missing alt is 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

KeyAction
Arrow keysMove the divider by 1 (Left and Right swap in right-to-left text)
Shift + arrow / Page Up / Page DownMove the divider by 10
Home / EndShow 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.