Build a before/after slider with HTML and CSS

Make a before and after image slider website that works with touch, a mouse, and arrow keys, then send the finished comparison as one link you can update later.

In short

Stack the two pictures in one box and clip the top layer at the slider position.

Use a range input as the control so touch, mouse, and keyboard input all have a working path.

Match the pictures' size and framing before you build the control. A slider cannot repair a mismatched viewpoint.

When the page is ready, share the working page rather than a screenshot so the recipient can move the divider.

A before/after slider is two pictures occupying the same rectangle. The lower layer stays visible. The upper layer is clipped, and a divider marks the clip edge. Moving one value changes the clipped area and the divider together.

That definition from the original guide is still the useful starting point. The missing part was a complete page that could be opened, tested on a narrow screen, shared, and changed at the same address. We completed that full path with a fictional studio renovation.

NOS Code view showing the fictional studio comparison HTML and its live preview
The page starts in Code view, with the HTML above and the working comparison preview beside the code.

What does a finished before and after image slider website need?

The comparison itself is only one part of the result. A useful page also needs clear Before and After labels, a visible divider, a control name for assistive technology, a short explanation of what changed, and a layout that fits a phone without sideways scrolling.

Our fictional page compares the same studio corner in both states. The window and desk stay aligned. The after side changes the wall, floor, lamp, and plant. Because both scenes share the same frame, the divider reveals a real visual difference instead of jumping between two viewpoints.

Download the complete fictional example

NOS View mode showing the studio before and after slider at 52 percent
View mode removes the code panel and leaves the interactive page at the full desktop width.

How do the two image layers stay together?

Give the comparison wrapper a fixed aspect ratio and make it the positioning reference. Put both picture layers at every edge of that wrapper. Then clip the upper layer from the right by the amount that is not currently revealed.

The divider reads the same percentage as the clip. At 30%, the divider sits 30% across the box and the upper layer occupies the left 30%. At 68%, it moves farther right and reveals more of the lower layer. Keeping one shared value prevents the line and the picture edge from drifting apart.

The example draws its fictional scene with HTML and CSS, so it has no external image files to lose. For photographs, place one image in each layer and give both the same width, height, and crop rule. Export both photographs at the same dimensions and shoot them from the same position. Keeping an image's aspect ratio explains the sizing side in more detail.

Why use a range input for the divider?

A range input already understands touch, mouse dragging, arrow keys, Home, and End. Stretch it over the comparison, give it a clear label, and use its value for the shared position. This is smaller and more dependable than inventing every drag and keyboard behavior separately.

The control in the example is named “Reveal after image.” Its current percentage is also exposed as readable text. A sighted reader gets the divider and the “68% after” readout; a keyboard or screen-reader user gets the named control and its current value.

Keep vertical page scrolling available on phones. A horizontal comparison should claim sideways movement, not every finger gesture that begins over the picture. The detailed control options are covered in Range slider in HTML.

How do you share the working comparison?

A screenshot freezes the divider. The recipient cannot reveal the hidden side or inspect the exact point where two details meet. We put the finished example in NOS, opened Share, and used the page's share address.

NOS Share panel for the fictional comparison page with Anyone with the link selected
The Share panel states that anyone with the link can read the page without signing in and cannot edit it.

The address from the Share panel is the one to send. The editing address is for the author, and the preview inside the editor is not the recipient's page. Keeping those roles separate avoids sharing an address that asks the reader to sign in.

Public desktop view of the fictional studio slider before the update at 52 percent
The shared page opened on a desktop screen with the original “Studio corner refresh” title and a 52% starting position.
Completed result: the shared page showed the same labels, divider, fictional scene, and explanation as the editor preview. It fit a 390-pixel-wide view with no horizontal overflow.

Yes, for this tested page. We kept the same NOS page and changed its HTML. The original shared address then showed the revised title and the new starting position.

NOS Edit mode open on the original fictional studio comparison
Edit mode opened on the same page and kept the original 52% comparison visible.

The first attempt in Edit mode selected visible text but did not change the underlying page. That is an important failure path for a custom interactive layout: if direct text editing does not reach the part you need, stop repeating the same click. Open Code, replace the page content there, and check the preview before leaving.

NOS Code view after the same page was updated to Studio corner makeover
After the real code update, the preview changed to “Studio corner makeover” and the divider started at 68%.

We then reopened the exact share address used before the edit.

Public desktop view of the updated studio slider at the same share address and 68 percent
The same shared address now showed the revised title and 68% starting position.

This is the practical reason to publish the comparison as a page. You can correct a label, replace the two pictures, or choose a better starting split without asking everyone to use a different address. Always reopen the shared page after an update; seeing the right preview in the editor is not proof that the reader's page changed.

What usually breaks a before/after slider?

  • The scene jumps at the divider: the pictures use different framing. Match their dimensions and camera position before changing the slider.
  • The layers appear one below the other: both layers must occupy the same positioned wrapper.
  • The box collapses while images load: give the wrapper an aspect ratio or an explicit height.
  • The line moves but the reveal does not: make the divider and the clipped layer read one shared position.
  • The page will not scroll past the slider on a phone: preserve vertical touch movement for the page.
  • Pictures disappear on the shared page: local file paths did not travel with the HTML. Embed the images or use image addresses the shared page can reach.

If the comparison belongs inside a set of project images rather than one focused page, HTML image gallery is the more useful next step.

When not to do this

Do not use a slider when the two images show different angles, crops, or moments that cannot be aligned. Put them side by side with captions instead. Side-by-side images are also better when readers need to see both states at once, print the comparison, or compare several details without dragging back and forth.

Where NOS fits

NOS turns the finished HTML page into a share link and keeps the editor and reader views separate. For this example, a reader could open the interactive comparison without signing in, while the author kept the editing controls. The current plan details are on the pricing page, and HTML to link covers the sharing step by itself.

Questions people ask

Can I make a before/after slider with only HTML and CSS?

You can build the two layers and a fixed split with HTML and CSS. A draggable range control still needs a small piece of browser logic to copy its changing value into the shared position. The downloadable example includes the complete working page.

Should I clip the top layer or change its width?

Clip the top layer. It keeps the picture at full size and changes only the visible area. Changing the layer width can squeeze the picture unless the inner image is forced to remain as wide as the full comparison box.

How do I use my own before and after photos?

Replace the two scene layers with image elements, give them the same box and crop rule, and use files with matching dimensions and framing. If the camera moved between shots, align the photographs before building the slider.

Will the same link show later edits?

It did in the tested NOS page: after the title and starting position changed, the original share address showed the revised page. Reopen the shared address after every edit to confirm the reader view updated.

Keep reading