Skip to content

Compare

Baseline 2025 Newly available

ARIA attribute reflection (ariaDescribedByElements, ariaLabelledByElements) is not yet Baseline widely available.

The component turns widely available in October 2027.

Works in Chrome 135+, Chrome Android 135+, Edge 135+, Firefox 136+, Firefox for Android 136+, Safari 17.4+, Safari on iOS 17.4+.

Why these versions:

Full browser support report

sv-compare compares two versions of the same content with a draggable divider, like photo edits.

Usage

<sv-compare aria-label="Photo edit">
<img slot="before" src="original.jpg" alt="The original photo" />
<img slot="after" src="edited.jpg" alt="The edited photo" />
</sv-compare>

Give both images an alt text, since both stay in the accessibility tree, even the part that the divider hides.

Layout

Both contents share one box, and the taller one sets its height. They fill the box with object-fit: cover unless your own styles set another object-fit. To fix the size of the box, set a height or an aspect-ratio on sv-compare itself:

sv-compare {
aspect-ratio: 16 / 9;
}

Since the handle stays centered on the divider, half of it sticks out at 0 and 100. To round the corners while keeping the handle whole, clip ::part(before) and ::part(after) instead of the host.

Set min and max to keep the divider away from the edges and both contents in view.

Captions

The divider hides each caption together with its content:

<sv-compare aria-label="Photo edit">
<img slot="before" src="original.jpg" alt="The original photo" />
<img slot="after" src="edited.jpg" alt="The edited photo" />
<span slot="before-label">Before</span>
<span slot="after-label">After</span>
</sv-compare>

Overlays

To lay a transparent image, like a drawing or a label layer, over a full one, set clip to the transparent one:

<sv-compare aria-label="Floor plan" clip="before">
<img slot="before" src="plan-lines.png" alt="The floor plan lines" />
<img slot="after" src="photo.jpg" alt="The room photo" />
</sv-compare>

Accessibility

The handle follows the WAI-ARIA slider pattern, and positionLabelHandler puts its value, position, in your own words:

compare.positionLabelHandler = position => `${Math.round(position)}% original`;

The handle is named "Divider" until aria-label or aria-labelledby on sv-compare names it, which tells the comparisons on a page apart. aria-describedby reaches the handle too, but no other aria-* attribute on sv-compare does.

A click on the content focuses the handle, without a focus ring.

Keyboard interaction

Key Behavior
ArrowRight / ArrowLeft Moves the divider by 1 toward the arrow, or down / up when vertical
ArrowUp / ArrowDown Increases / decreases by 1, or moves up / down when vertical
Shift + arrow key Moves by 10 instead of 1
PageUp / PageDown Increases / decreases by 10, or moves up / down when vertical
Home Moves to min
End Moves to max

Pointer interaction

Since a press anywhere on the component moves the divider, keep links and buttons out of the content.

  • A drag moves the divider with the pointer, with no animation.
  • A click or a tap moves the divider to that point, with an animation.
  • A press on the handle does not make the divider jump to the pointer.
  • A swipe across the axis scrolls the page and puts the divider back where the press started.

Structure

div
divbefore
slotbefore - Your before content
slotbefore-label - Your caption in the top start corner
divafter
slotafter - Your after content
slotafter-label - Your caption in the top end corner, or the bottom start corner when vertical
divdivider
divhandle
slothandle
svg - Default arrows

API References

Methods

setPosition()

Moves the divider to position, skipping the animation when isInstant is true.

Properties

position

The divider position, in percent from the start edge. 0 shows only the after content, and 100 only the before content. The value is clamped to min–max, and a value that is not a number gives 50. Setting it animates the divider.

min

The lowest position, from 0 to 100.

max

The highest position, from 0 to 100, where a value below min acts as min.

positionLabelHandler

Callback that returns the text that assistive technology reads for a position. Without it, the text is the rounded percentage, like 50%.

vertical

Whether the before content is on top, and the divider moves up and down.

followPointer

Whether a hovering mouse or pen moves the divider without a press.

disabled

Whether user interaction is blocked. The handle leaves the tab order but stays in the accessibility tree.

Attributes

"clip"

Which content the divider clips. With before or after, only that content is clipped, and it lies over the whole other one.

"follow-pointer"

Moves the divider with a hovering mouse or pen, without a press.

"position"

The divider position, in percent from the start edge. 0 shows only the after content, and 100 only the before content. The value is clamped to min–max, and a value that is not a number gives 50. Setting it animates the divider.

"min"

The lowest position, from 0 to 100.

"max"

The highest position, from 0 to 100, where a value below min acts as min.

"vertical"

Whether the before content is on top, and the divider moves up and down.

"disabled"

Whether user interaction is blocked. The handle leaves the tab order but stays in the accessibility tree.

Events

positionchange

Fired after position changes, by the user, from code, or by a min or max change that moves it.

adjusted

Fired when the user moves the divider with a drag, a click, or a key, before it applies.

Slots

before

The content on the start side, like the original image.

after

The content on the end side, like the edited image.

before-label

A caption in the top start corner, clipped to the start side of the divider, whatever clip is.

after-label

A caption in the top end corner, or the bottom start corner when vertical, clipped to the end side of the divider, whatever clip is.

handle

Content in place of the arrows in the handle, hidden from assistive technology.

CSS Properties

--sv-compare-handle-bg

Background color of the handle.

--sv-compare-handle-clr

Color of the arrows in the handle.

--sv-compare-shadow

Shadow that keeps the divider line and the handle visible on light content.

--sv-compare-ease

Easing of the divider animation.

--sv-compare-label-bg

Background color of the captions.

--sv-compare-label-clr

Text color of the captions.

--sv-compare-divider-clr

Color of the divider line and the handle border.

--sv-compare-divider-thickness

Thickness of the divider line and the handle border.

--sv-compare-handle-sz

Size of the handle.

--sv-compare-dur

Duration of the divider animation after a click, a key, or a position change.

CSS Parts

::part(before)

The layer of the before content, clipped to the start side of the divider.

::part(after)

The layer of the after content, clipped to the end side of the divider.

::part(divider)

The line between the two contents.

::part(handle)

The knob on the divider, the slider that takes the focus.

CSS States

:state(--active)

The divider follows the pointer: during a drag, or while a pointer hovers with follow-pointer.