Experience the powerful AI writing right inside WordPress
Show stunning before-and-after transformations with image sliders.
Improve user engagement by showing estimated reading time.
Written by Mahmuda Akter Isha
Showcase Designs Using Before After Slider.
A WP before after image slider jQuery CodePen example is one of the easiest ways to test an image comparison slider before adding it to a WordPress website. Whether you are building a photography portfolio, renovation case study, beauty clinic website, product comparison section, or design showcase, a before-after slider helps visitors compare two images in a simple visual way.
Instead of showing two separate images side by side, a before-after image slider lets users drag a handle to reveal the “before” and “after” versions in the same frame. This makes the difference easier to understand and more engaging for website visitors.
In this guide, you will learn how to create a responsive before after image slider using HTML, CSS, jQuery, and CodePen-style code, then add it to WordPress. You will also learn how to use it with Gutenberg, Elementor, custom shortcode methods, and plugin alternatives.
By the end, you will know how to:
A WP before after image slider is an image comparison tool that lets users compare two images by dragging a slider handle. One image usually shows the “before” version, and the other image shows the “after” version.
For example, you can use it to compare:
The main benefit is that users do not need to look at two separate images and guess the difference. They can drag the handle and see the change directly.
A before after image slider WordPress code solution is useful when you want more control over the design, layout, and behavior of the slider. However, a plugin can be better when you want a faster no-code setup.
A before after image slider jQuery CodePen example is useful because it allows you to test the slider outside WordPress before adding it to your website.
CodePen is commonly used by developers and designers to test HTML, CSS, and JavaScript snippets. You can build the slider, check how it works, adjust the design, and then move the final code into WordPress.
Using jQuery is helpful because WordPress already includes jQuery by default. That means you can often build interactive WordPress elements without loading a large extra JavaScript library.
A jQuery before after image comparison slider is especially useful when you want:
However, you need to add the code correctly in WordPress. A slider that works in CodePen may not work in WordPress if the jQuery is not wrapped properly, the image URLs are wrong, or the theme has conflicting CSS.
Below is a simple responsive before after image slider using HTML, CSS, and jQuery. You can test this in CodePen first, then move it into WordPress.
This version is designed to be more flexible than fixed-width examples. It uses a percentage-based slider position, so it works better on different screen sizes.
<div class="cc-before-after-slider"> <div class="cc-before-after-wrapper"> <img src="https://yourwebsite.com/wp-content/uploads/before-image.jpg" alt="Before image" class="cc-before-img" > <div class="cc-after-layer"> <img src="https://yourwebsite.com/wp-content/uploads/after-image.jpg" alt="After image" class="cc-after-img" > </div> <div class="cc-slider-line"> <span class="cc-slider-handle"></span> </div> </div> <div class="cc-before-after-labels"> <span>Before</span> <span>After</span> </div> </div>
.cc-before-after-slider { width: 100%; max-width: 900px; margin: 30px auto; font-family: Arial, sans-serif; } .cc-before-after-wrapper { position: relative; width: 100%; overflow: hidden; border-radius: 16px; cursor: ew-resize; user-select: none; } .cc-before-after-wrapper img { display: block; width: 100%; height: auto; pointer-events: none; } .cc-before-img { position: relative; z-index: 1; } .cc-after-layer { position: absolute; top: 0; left: 0; width: 50%; height: 100%; overflow: hidden; z-index: 2; } .cc-after-img { height: 100%; width: auto; max-width: none; } .cc-slider-line { position: absolute; top: 0; left: 50%; width: 3px; height: 100%; background: #ffffff; z-index: 3; transform: translateX(-50%); } .cc-slider-handle { position: absolute; top: 50%; left: 50%; width: 46px; height: 46px; background: #ffffff; border-radius: 50%; transform: translate(-50%, -50%); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25); } .cc-slider-handle::before, .cc-slider-handle::after { content: ""; position: absolute; top: 50%; width: 0; height: 0; transform: translateY(-50%); } .cc-slider-handle::before { left: 12px; border-top: 7px solid transparent; border-bottom: 7px solid transparent; border-right: 8px solid #333333; } .cc-slider-handle::after { right: 12px; border-top: 7px solid transparent; border-bottom: 7px solid transparent; border-left: 8px solid #333333; } .cc-before-after-labels { display: flex; justify-content: space-between; margin-top: 10px; font-weight: 600; color: #333333; } @media (max-width: 600px) { .cc-slider-handle { width: 38px; height: 38px; } .cc-before-after-labels { font-size: 14px; } }
jQuery(document).ready(function ($) { $(".cc-before-after-wrapper").each(function () { const $wrapper = $(this); const $afterLayer = $wrapper.find(".cc-after-layer"); const $sliderLine = $wrapper.find(".cc-slider-line"); let isDragging = false; function moveSlider(clientX) { const wrapperOffset = $wrapper.offset().left; const wrapperWidth = $wrapper.width(); let position = ((clientX - wrapperOffset) / wrapperWidth) * 100; position = Math.max(0, Math.min(100, position)); $afterLayer.css("width", position + "%"); $sliderLine.css("left", position + "%"); } $wrapper.on("mousedown touchstart", function (e) { isDragging = true; const clientX = e.type === "touchstart" ? e.originalEvent.touches[0].clientX : e.clientX; moveSlider(clientX); }); $(document).on("mousemove touchmove", function (e) { if (!isDragging) return; const clientX = e.type === "touchmove" ? e.originalEvent.touches[0].clientX : e.clientX; moveSlider(clientX); }); $(document).on("mouseup touchend", function () { isDragging = false; }); }); });
This code creates a simple draggable before-after slider. The after image is placed on top of the before image, and the visible width changes as users drag the handle.
The slider works with three main layers:
The before image stays visible in the background. The after image is placed above it inside a container with overflow: hidden. When the user drags the handle, jQuery changes the width of the after image container.
overflow: hidden
For example, when the slider position is 50%, half of the after image is visible. When the slider moves to 80%, more of the after image appears. When it moves to 20%, more of the before image is visible.
This is why it is important to use two images with the same dimensions. If the before and after images have different sizes, the comparison may look broken or misaligned.
Before adding the slider to WordPress, test it in CodePen or any local HTML editor.
Follow these steps:
Testing the image comparison slider in CodePen helps you confirm that the HTML, CSS, and jQuery are working before you move the code into WordPress.
This is especially useful because a CodePen slider may work perfectly in the editor but need small adjustments when added to WordPress themes, page builders, or custom layouts.
Once your CodePen demo works, you can add the same before after image slider to WordPress.
There are several ways to do this:
The best method depends on your technical comfort level and how many sliders you want to add.
This is the simplest no-plugin custom code method.
Go to:
WordPress Dashboard → Media → Add New
Upload both images. Try to use images with the same width and height. For example:
After uploading, copy each image URL from the Media Library.
Open the WordPress page or post where you want to show the slider.
Add a Custom HTML block and paste the HTML code:
<div class="cc-before-after-slider"> <div class="cc-before-after-wrapper"> <img src="PASTE-BEFORE-IMAGE-URL-HERE" alt="Before website redesign example" class="cc-before-img" > <div class="cc-after-layer"> <img src="PASTE-AFTER-IMAGE-URL-HERE" alt="After website redesign example" class="cc-after-img" > </div> <div class="cc-slider-line"> <span class="cc-slider-handle"></span> </div> </div> <div class="cc-before-after-labels"> <span>Before</span> <span>After</span> </div> </div>
Replace the placeholder image URLs with your own WordPress Media Library URLs.
Appearance → Customize → Additional CSS
Paste the CSS code from the earlier section.
This controls the slider layout, handle, image positioning, responsive behavior, and labels.
For JavaScript, it is better to use a code snippets plugin or your child theme file instead of pasting scripts directly into the page editor.
Add the jQuery code from the earlier section using one of these methods:
Make sure the code uses this wrapper:
jQuery(document).ready(function ($) { // Your slider code here });
This is important because WordPress loads jQuery in no-conflict mode. Using $ directly without wrapping it properly can cause the before after slider not working issue.
$
After adding the code, test the page on:
Also test dragging the slider with a mouse and with touch gestures.
To add a before after image slider in Gutenberg, use the Custom HTML block method.
Here is the process:
This method is useful when you only need one or two sliders and you are comfortable adding custom code.
However, Gutenberg does not automatically manage the CSS and JavaScript for you. That means you must make sure the required CSS and jQuery code are properly loaded on the page.
For multiple sliders, use unique wrapper classes or the reusable code above, which already supports multiple sliders on the same page.
Elementor users can also add a custom jQuery before after image slider using the HTML widget.
This method works well when you want to place the slider inside an Elementor landing page, service page, portfolio page, or case study layout.
You can also style the section around the slider with Elementor controls, such as spacing, background color, heading text, and call-to-action buttons.
For non-technical Elementor users, a dedicated before after image slider plugin with Elementor support may be easier than adding custom code.
A shortcode is useful when you want to reuse the same slider layout in multiple places.
You can create a shortcode that outputs the slider HTML. This is a better approach than copying the same HTML again and again.
Here is a simple example:
function cc_before_after_slider_shortcode($atts) { $atts = shortcode_atts( array( 'before' => '', 'after' => '', 'before_alt' => 'Before image', 'after_alt' => 'After image', ), $atts, 'before_after_slider' ); ob_start(); ?> <div class="cc-before-after-slider"> <div class="cc-before-after-wrapper"> <img src="<?php echo esc_url($atts['before']); ?>" alt="<?php echo esc_attr($atts['before_alt']); ?>" class="cc-before-img" > <div class="cc-after-layer"> <img src="<?php echo esc_url($atts['after']); ?>" alt="<?php echo esc_attr($atts['after_alt']); ?>" class="cc-after-img" > </div> <div class="cc-slider-line"> <span class="cc-slider-handle"></span> </div> </div> <div class="cc-before-after-labels"> <span>Before</span> <span>After</span> </div> </div> <?php return ob_get_clean(); } add_shortcode('before_after_slider', 'cc_before_after_slider_shortcode');
Then you can use this shortcode inside a page or post:
[before_after_slider before="https://yourwebsite.com/before.jpg" after="https://yourwebsite.com/after.jpg" before_alt="Before renovation" after_alt="After renovation"]
This shortcode method is useful for developers or advanced WordPress users. For beginners, a plugin is usually easier and safer.
Many users search for a before after image slider CodePen example, build the slider successfully, and then face problems when moving it to WordPress.
Here is the correct process:
Copy the HTML from CodePen and paste it into a WordPress Custom HTML block, Elementor HTML widget, or shortcode template.
Copy the CSS and paste it into:
Copy the JavaScript and add it through:
Do not randomly paste JavaScript into the visual editor because WordPress may remove or break the script.
CodePen often uses external image URLs. In WordPress, use image URLs from your Media Library for better control and performance.
In CodePen, $ may work directly. In WordPress, use:
jQuery(document).ready(function ($) { // Code here });
After adding the code, clear:
Sometimes the slider is working, but an older cached file prevents the update from showing.
The code in this guide already supports multiple sliders because it uses:
$(".cc-before-after-wrapper").each(function () { // Slider code });
That means you can add more than one slider on the same page using the same HTML structure.
Example:
<div class="cc-before-after-slider"> <!-- First slider images --> </div> <div class="cc-before-after-slider"> <!-- Second slider images --> </div> <div class="cc-before-after-slider"> <!-- Third slider images --> </div>
Each slider will work independently.
This is useful for:
When adding multiple sliders, avoid using very large images for every slider. Large images can slow down the page and affect user experience.
Custom jQuery code is useful, but it is not always the best option for every WordPress user.
A WordPress before after image slider plugin can save time, especially when you want:
For many site owners, marketers, agencies, and non-technical users, a plugin is faster than writing or managing custom code.
A plugin can also reduce the risk of breaking your layout, especially when your WordPress theme or page builder updates.
Use custom jQuery code when you want full control. Use a plugin when you want speed, flexibility, and easier management.
Both methods can work well. The best choice depends on your goal.
Choose custom code when you only need a lightweight slider and you are comfortable editing HTML, CSS, and jQuery.
Choose a plugin when you need multiple sliders, client-friendly controls, page builder support, or a faster no-code workflow.
A visual comparison slider can work in many industries. Here are some practical examples.
Photographers can use a before after slider to show photo retouching, color correction, lighting adjustment, or editing style.
Real estate businesses can compare empty rooms with staged rooms, old property photos with renovated spaces, or exterior improvements.
Beauty clinics can show treatment results, skincare improvements, smile makeovers, hair restoration progress, or cosmetic service outcomes.
Contractors, interior designers, and home renovation companies can show room transformations, kitchen upgrades, bathroom remodels, and landscape improvements.
Web designers can compare old website designs with new website redesigns. This is useful for case studies and service pages.
Brands can show product improvements, photo editing results, cleaning results, repair results, or feature comparisons.
A before after image slider can make landing pages more persuasive by showing visible transformation instead of only describing it with text.
A slider is only effective when it is easy to use and visually clear. Follow these best practices for better results.
Always use before and after images with the same width and height. Different image sizes can cause alignment issues.
Large images can slow down your WordPress page. Compress images before uploading them to the Media Library.
Use helpful alt text for both images. Instead of writing “before image,” describe what the image shows.
Many visitors browse from mobile devices. Make sure the slider handle is easy to drag on smaller screens.
Using too many image sliders can slow down the page. Add only the most important comparisons.
Labels like “Before” and “After” help users understand what they are seeing.
Test the slider in Chrome, Safari, Firefox, and mobile browsers.
The slider should highlight the image comparison, not distract users with too many effects.
This usually happens because the jQuery code is not loaded correctly.
Common causes include:
Use this wrapper in WordPress:
jQuery(document).ready(function ($) { // Slider code here });
Also clear your cache after adding the code.
This is a common issue. CodePen automatically manages JavaScript settings, but WordPress does not always behave the same way.
To fix it:
If the slider handle is visible but does not move, the JavaScript is probably not running.
Check these points:
Class names must match exactly between your HTML, CSS, and JavaScript.
Image alignment issues usually happen when the before and after images have different dimensions.
For best results, prepare both images in the same canvas size before uploading them to WordPress.
If the slider looks broken on mobile, check the CSS.
Common fixes:
width: 100%
The code in this guide uses percentage-based width, which is better for responsive WordPress layouts.
The handle may not move if JavaScript cannot calculate the slider width correctly.
Possible causes:
Try placing the slider in a normal visible section first. Once it works, move it into your desired layout.
Large images are the most common reason for slow sliders.
To improve speed:
A before after image comparison slider is visual, so image quality matters. But the images should still be optimized for the web.
A before after slider is visual, but you can still make it more accessible.
Use these tips:
For example, instead of only showing a slider, you can add a short caption:
“Before: old kitchen layout with dark cabinets. After: renovated kitchen with open layout and modern lighting.”
This helps users understand the transformation even if they cannot fully interact with the slider.
Image sliders can affect performance if not optimized properly. Since before-after sliders use two images in the same space, you should pay extra attention to file size.
Follow these performance tips:
Do not upload a 4000px image if your content area only displays 900px. Resize the image before uploading.
Use compressed JPG, PNG, or WebP images. Smaller file sizes help the page load faster.
If you add several sliders at the top of the page, the page may feel slow. Place the most important slider first and move extra comparisons lower on the page.
Lazy loading helps delay off-screen images until users scroll near them. This is useful when you have multiple before-after sliders on one page.
The custom jQuery code in this tutorial is lightweight. Avoid loading multiple slider libraries when one simple solution is enough.
You can place a before-after image slider in different parts of your site depending on your goal.
Use sliders to show service results. For example, a renovation company can show before and after remodeling photos.
Showcase multiple projects with image comparisons.
Use a slider to support a story. Explain the problem, show the before image, describe the process, and reveal the after result.
Place one strong before-after comparison near the top or middle of the landing page to increase trust.
Use sliders inside tutorials, reviews, transformation stories, and comparison articles.
Show product improvement, cleaning results, editing results, or feature differences.
A slider works best when the visual change is clear and meaningful.
Most before-after sliders use a horizontal drag handle. This means users drag the handle from left to right.
A horizontal slider is best for:
A vertical before-after slider lets users drag from top to bottom.
A vertical slider can be useful for:
For most WordPress websites, a horizontal before after image slider is the easiest and most familiar option.
Once the basic slider is working, you can customize it to match your website design.
You can adjust the handle size, shape, shadow, and icon.
.cc-slider-handle { width: 54px; height: 54px; border: 3px solid #ffffff; }
For a softer card-style look:
.cc-before-after-wrapper { border-radius: 24px; }
For a sharp modern look:
.cc-before-after-wrapper { border-radius: 0; }
.cc-before-after-wrapper { box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12); }
You can replace “Before” and “After” with more specific labels, such as:
A short caption can help explain what changed.
<p class="cc-slider-caption"> Drag the handle to compare the original room with the completed renovation. </p>
.cc-slider-caption { margin-top: 12px; font-size: 15px; color: #555555; text-align: center; }
Custom code gives you control, but it also requires maintenance.
A custom jQuery before after slider may not be the best choice when:
In those cases, a WordPress before after slider plugin is usually a better option. It gives you a faster workflow and reduces the chance of code mistakes.
A WP before after image slider jQuery CodePen example is a great starting point when you want to build a custom image comparison slider for WordPress. You can test the HTML, CSS, and jQuery in CodePen first, then move the code into WordPress using a Custom HTML block, Elementor HTML widget, shortcode, or theme file.
For developers, the custom jQuery method gives full control over the slider structure and design. For beginners, agencies, marketers, and busy website owners, a WordPress before after image slider plugin is often easier and faster.
The most important thing is to choose the method that fits your workflow. Use custom code when you want flexibility. Use a plugin when you want speed, page builder support, shortcode options, and easier management.
A well-designed before after image slider can make your WordPress page more visual, more convincing, and more useful for visitors. Whether you are showing a renovation, redesign, product change, photo edit, or service result, the right image comparison slider can help users understand the transformation instantly.
You can create a WP before after image slider using HTML for the structure, CSS for the layout, and jQuery for the drag effect. First, test the code in CodePen. Then move the HTML, CSS, and jQuery into WordPress using a Custom HTML block, Additional CSS, and a code snippets plugin or theme script file.
Yes, you can use CodePen before after slider code in WordPress. You need to copy the HTML, CSS, and jQuery into the correct WordPress areas. Also, make sure the jQuery code is wrapped properly for WordPress no-conflict mode.
You can add a before after image slider in WordPress without a plugin by using custom HTML, CSS, and jQuery. Add the HTML inside a Custom HTML block, add CSS in Additional CSS, and add jQuery through a code snippets plugin or child theme file.
Your before after slider may not work because jQuery is not loaded, the script is placed incorrectly, the $ symbol is not wrapped properly, the image URLs are wrong, or another plugin is causing a conflict. Use jQuery(document).ready(function ($) { }) to make the code work properly in WordPress.
jQuery(document).ready(function ($) { })
A CodePen slider may work because CodePen loads scripts differently. In WordPress, you need to load jQuery correctly, add JavaScript in the right place, use WordPress-compatible jQuery syntax, and clear your cache after changes.
Use percentage-based widths instead of fixed pixel values. Set the slider container to width: 100%, make images responsive, and control the after image layer with percentage width. Also test the slider on mobile devices.
Yes, you can add a before after image slider in Elementor using the HTML widget. Paste the slider HTML into the widget, add the CSS to your site, and load the jQuery through Elementor custom code, a snippets plugin, or your theme.
This page was last edited on 23 June 2026, at 5:50 pm
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
How many people work in your company?Less than 1010-5050-250250+
By proceeding, you agree to our Privacy Policy