Before and after slider

Before and after slider

This tutorial shows how to build a before and after image slider that reveals the new image under the cursor, then turn it into a component you can reuse across your site.

This before and after image slider WordPress build lets visitors see a transformation happen right under their mouse. Move the cursor and the old image turns into the new one, no slider plugin and no code needed.

It’s built from two images, a few nested divs, and a single mouse move interaction, then saved as a component so you can drop it anywhere and still tweak each copy on its own.

Set up the before and after slider

Add a wrapper div

From the Add panel, drop a Div into your section. Any heading or paragraph in the section is optional, the effect doesn’t depend on them.

Rename the div

Right-click the div and choose Rename, or double-click its label in the Navigator. Name it Before after wrapper to keep the structure easy to follow.

Set the position to relative

In the Style panel, set Position to Relative. Any absolutely positioned element inside it will now position itself relative to this wrapper.

Add the before image

From the Add panel, add an Image element to the wrapper, then select your before image from the media library.

Make the image full width

Set the image’s width to 100% and its height to auto so it stretches across the wrapper. If your theme adds a border radius to all images, set it to 0 here.

Add the after image

Add the after wrapper

From the Add panel, add another Div inside the Before after wrapper and rename it After wrapper.

Stretch it over the before image

In the Style panel, set Position to Absolute and set each single position property (top, right, bottom, left) to 0. Then set Overflow to Hidden.

Add the after image

Duplicate the before image, drop the copy into the After wrapper, and replace it with your after image.

Animate the before and after slider

Add a mouse move interaction

Select the Before after wrapper, open the Interaction tab, and add a Mouse move interaction.

Move the after wrapper

Select the After wrapper in the Navigator as the target. For the action, add Transform, then Translate, then Translate X, and set To to 100%. On its own, this pushes the after image out of view.

Counter-move the after image

Select the image inside the After wrapper and add a Translate X action to it too, with To set to -100%. The image now stays in place while its wrapper moves, so the after image is revealed as the mouse moves.

Track only the image area

In the interaction settings, change Tracking from Whole page to Trigger element. The image comparison slider now only reacts while the mouse is over the images, not anywhere on the page.

Remove the lag

Find the Smoothing option in the interaction settings. Any value above 0 makes the reveal trail behind the cursor (it was 60% in the video), so set it to 0 for an instant follow.

Add a divider

Add the divider wrapper

From the Add panel, drop in another Div and rename it Divider wrapper. Set Position to Absolute, raise the Z-index a little so it sits on top, and set each single position property to 0 so it stretches across the area.

Create the divider

Duplicate the Divider wrapper, place the copy inside it, and rename it Divider. Give it a background color, for example white.

Set the width with a variable

The divider now covers the whole area, so limit it with a max width. Instead of typing a value, create a new variable named Vertical divider with a value of 6px. Copy its CSS custom property, then press Enter to confirm.

Center the divider with calc()

Setting Left to 50% would leave the divider slightly off center, since it doesn’t account for the divider’s own width. Mosaic supports CSS functions like calc(), so set Left to calc(50% – var(–your-variable) / 2), pasting in the custom property you copied. Because it uses the variable, the divider stays centered even when you change the Vertical divider value later.

Animate the divider

Select the Divider wrapper in the Navigator and add a Transform action with Translate, then Translate X. Since the divider starts in the center, set Initial to -50% and To to 50%. The divider now moves along the edge between the two images.

Make it a component

Create a component

Copying and pasting the slider doesn’t scale, since every later change would have to be made in each copy. Instead, create a component from it. Name it Before after, set the main category to Part, and pick a category or make a new one (Uncategorized works too). Click Create, and the slider on the page becomes an instance of the component.

Reuse it on another page

From the Add panel, drop a Component element where you want the slider, then select the Before after part component. Remove any old image it replaces.

Open the main component

If the instance doesn’t fill the available height, click the component icon in the toolbar to enter the original component. Any change you make here affects every instance across your site.

Set the height to 100%

The component’s outer container has no set width or height, so its children decide its size. In a narrower container, the images keep their aspect ratio and scale down, which makes the slider shorter. Set its height to 100% so it fills the space. The images may get cropped as a result, which works fine as the default.

Adjust one instance

Enter the instance scope

Leave the component scope and enter the scope of the specific instance instead. Changes made now only affect this instance.

Change how the image fits

Select the image, then open the more options at the bottom of the Style panel to find Object fit. Change it from Cover to Contain to keep the full image visible. Or keep Cover and adjust the focus point, for example to center left, so that part of the image always stays in view.

Replace the images

Replace the before image with the one this instance needs. The after image sits hidden under it in the editor, so temporarily hide the other image in the editor to bring it into view, then replace it too.

Result

Your before and after image slider is ready. When visitors move their mouse over it, the after image slides into view and follows the cursor instantly, with a thin divider tracking the edge between the two. Since it’s a component, you can reuse it anywhere, update every copy from one place, and still adjust a single instance when you need to.

Last updated: October 21, 2026

Still have more questions? Let us help!

Your cookie preferences

We use cookies to improve your experience, analyze traffic, and personalize content. By clicking "Accept all" you agree to storing them on your device. Read our privacy policy.