<cs-comparison>
StableMediaSince 0.1

Comparisons show the visual differences between two pieces of similar content using a draggable divider. Use them for before/after images, design revisions, or side-by-side previews.

Compare two pieces of content with a divider users can drag across them. It’s most often used for before/after images — for best results, the two sides should share the same dimensions. Any content works, though; see the theme page for a full-UI example.

Grayscale version of kittens in a basket looking around. Color version of kittens in a basket looking around.

The divider is keyboard accessible.
Focus it and use the arrow keys — shift + arrow moves in larger steps, and home / end jump to either end.

Examples

Initial Position

Use the position attribute to set the initial position of the slider. This is a percentage from 0 to 100.

A person sitting on bricks wearing untied boots. A person sitting on a yellow curb tying shoelaces on a boot.

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/comparison/comparison.js';
import './cornerstone/components/comparison/comparison.js';
import CsComparison from '@cruglobal/cornerstone-components/react/comparison/index.js';

Slots

Learn more about using slots.

NameDescription
afterThe after content, often an <img> or <svg> element.
beforeThe before content, often an <img> or <svg> element.
handleThe icon used inside the handle.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
positionpositionThe position of the divider as a percentage.number50Yes

Events

Learn more about events.

NameDescription
changeEmitted when the position changes.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--divider-widthThe width of the dividing line.
--handle-sizeThe size of the compare handle.

Custom States

Learn more about custom states.

NameDescriptionCSS selector
draggingApplied when the comparison is being dragged.:state(dragging)

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
afterThe container that wraps the after content.::part(after)
beforeThe container that wraps the before content.::part(before)
comparisonThe component's outer wrapper.::part(comparison)
dividerThe divider that separates the before and after content.::part(divider)
handleThe handle that the user drags to expose the after content.::part(handle)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.