shadcn.io is an independent project built on shadcn/ui
Components
Free
Comparison for Svelte UI SDK
Two images or layouts in one frame, split by a divider people drag, hover or move with the arrow keys. A before and after slider for Svelte on UI SDK.
On this page9 sections
Comparison stacks two versions of the same picture in one frame and splits them with a divider, so moving it reveals more of one and less of the other. Reach for it when the difference is the point: a photo edit, a restored scan, a screen before and after a redesign.
To show two pictures one after the other instead, use a Carousel, where each gets the whole frame.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
The layer whose position is start shows on the divider's start side, left in left-to-right text, and the end layer fills the rest. Give both the same size and crop so they line up.
<Comparisonclass="aspect-video max-w-lg rounded-xl">
<ComparisonItemposition="start">
<imgalt="Before: the valley in black and white"class="size-full object-cover grayscale"src="/valley.jpg"
/>
</ComparisonItem>
<ComparisonItemposition="end">
<imgalt="After: the valley in color"class="size-full object-cover"src="/valley.jpg"
/>
</ComparisonItem>
<ComparisonHandle />
</Comparison>
Accessibility
ComparisonHandle is a focusable slider from 0 to 100, labeled Divider position unless you pass aria-label, and a screen reader reads its value as a percentage, such as 25%. Give it a label that names the two versions, as the Content demo does. Both layers stay in the page however much the divider hides, so each image's alt should say which version it is. The arrow keys move the divider the way they point, so in right-to-left text ArrowRight lowers the value.
KeyAction
Tab
Moves focus to the handle.
Arrow Right
Moves the divider right by step, 1 percent unless you set it. Hold Shift to move ten times as far.
Arrow Left
Moves the divider left by step. Hold Shift to move ten times as far.
Arrow Up
Moves the divider one step toward the end edge. Hold Shift to move ten times as far.
Arrow Down
Moves the divider one step toward the start edge. Hold Shift to move ten times as far.
Page Up
Moves the divider ten steps toward the end edge.
Page Down
Moves the divider ten steps toward the start edge.
Home
Moves the divider to the start edge, which shows only the end layer.
End
Moves the divider to the end edge, which shows only the start layer.
Demos
Before and after labels
A Before chip sits in the top start corner of the black and white layer and an After chip in the top end corner of the color one, so each names the side it is on and slides out of view with its layer as the divider passes. The handle's aria-label says what it compares. Use it whenever the two versions aren't obvious at a glance.
Hover
With mode set to hover, the divider follows the mouse across the photo with no press, and the grip shows only while the handle has keyboard focus. A finger still drags, since a touch screen has no hover. Pick it for a quick scan of a color grade, and keep the default drag mode when people need to stop at an exact spot.
Custom handle
Children of ComparisonHandle replace the default line and grip, here with a thin rule and a round frosted knob around a two-way chevron, and defaultValue starts the divider at 35. The handle still places itself, takes the keys and stays a slider. Draw your own focus style with the group-focus-visible/comparison-handle: variant, as the knob does, since the default ring belongs to the default grip.
Controlled
The position lives in the demo's state: value passes it in, the change callback writes back every move, and five buttons under the photo set it to 0, 25, 50, 75 or 100 percent, beside a readout of the current value. Control it to tie the divider to the rest of the page, such as stops for a guided tour of an edit.
Content
The layers hold layout instead of photos: a dashed wireframe of a card on the start side and the finished card on the end side, laid out alike in a 4:3 frame so the divider lines them up. The handle's aria-label names what it compares. The card's Open preview label is a styled span, since the layers ignore the pointer and a real button there couldn't be clicked.
FAQ
What's the difference between drag and hover mode in Comparison?
drag, the default, moves the divider while a pointer is pressed, and a click anywhere in the frame jumps it to that spot. hover follows a mouse with no press, which is quicker for a scan and harder to stop at an exact place, and it hides the grip unless the handle has keyboard focus.
Touch drags in both modes, as a touch screen has no hover. The Hover demo shows the second mode.
Does the Comparison slider work on phones?
Yes. A finger drags the divider from anywhere in the frame, and the page still scrolls up and down, because the frame claims only sideways moves through touch-pan-y. The handle is also a slider for keyboards and screen readers, so the comparison works without a pointer at all.
On a narrow screen a 16:9 frame is short, and a taller ratio, such as aspect-square below the sm breakpoint, gives the difference more room.
Which side is the start layer on in a Svelte Comparison?
start shows on the divider's start side and end on its end side: left and right in left-to-right text, right and left in right-to-left text. The value counts from the start edge, so 25 shows a quarter of the start layer and three quarters of the end one.
The direction comes from the page or any ancestor's dir, so the same markup mirrors in Arabic or Hebrew, and the arrow keys follow it.
How do I customize the Comparison divider handle?
Pass children to ComparisonHandle and they replace the default line and grip, as in the Custom handle demo. The handle keeps its place on the divider, its keys and its slider role, and a press anywhere in the frame still moves the divider, so the knob is a cue rather than the only place to grab.
Give your knob a focus style through the group-focus-visible/comparison-handle: variant so keyboard users can see where they are. To restyle the default instead, add classes to ComparisonHandle or edit its line and grip in components/ui/comparison.
How do I set or read the Comparison slider position?
Pass value and onValueChange to Comparison, as the Controlled demo does, or only defaultValue to start somewhere other than 50. onValueChangeEnd receives the position once a drag ends, on each key press, and when a mouse leaves the frame in hover mode, which suits saving it.
The position is also the --comparison-position CSS variable on the frame, as a percentage, so a caption or an overlay can follow the divider without any script.
How do I add Before and After labels to a Comparison slider?
Put a small label inside each ComparisonItem and pin it to that layer's outer top corner, such as absolute top-3 start-3 z-10 in the start layer and absolute top-3 end-3 z-10 in the end layer, as the Before and after labels demo does. The z-10 keeps it above an image with a filter such as grayscale, which would otherwise paint over it. Each label is clipped with its layer, so it slides out of view as the divider passes over it and returns when the divider moves back.
A screen reader reads both labels and both images in order, whatever the divider shows, so make each label and the alt of its image agree.
Can Comparison compare HTML or a video instead of images?
Yes. A ComparisonItem takes any content: a video, a chart, a page before and after a redesign, as in the Content demo. The layers are stacked and clipped to the divider, so give both the same layout and size.
The layers ignore the pointer, so links and buttons inside them can't be clicked, and a video in one can't be started by clicking it. Put its controls outside the frame.
API Reference
Comparison
PropType
value?number
The divider's position, in percent from the start edge (controlled). Two-way with bind:value.
defaultValue?number
Where the divider starts when uncontrolled, in percent from the start
edge.
Default
50
onValueChange?(value: number) → void
Called with the new position, in percent, as the divider moves.
Parameters
value – number
Returns
void
mode?ComparisonMode
drag moves the divider while a pointer is pressed (a click jumps it
there); hover follows a mouse without a press. Touch drags in both.
Default
"drag"
onValueChangeEnd?(value: number) → void
Called with the position once a drag or a key press ends.
Parameters
value – number
Returns
void
step?number
How far an arrow key moves the divider, in percent; Shift, Page Up and
Page Down move ten times as far.
Default
1
class?string
Merged with the part's classes.
CSS variableType
--comparison-positionstring
The divider's place from the start edge (50%).
ComparisonItem
<div>data-slot="comparison-item"
PropType
position"start" | "end"
The divider's side this layer shows on: start (left in left-to-right
text) or end.