Super Simple Image Comparison Slider in HTML

Super Simple Image Comparison Slider in HTML
Code Snippet:Easy comparison slider
Author: Adam Argyle
Published: March 4, 2024
Last Updated: March 4, 2024
Downloads: 457
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a super simple image comparison slider in HTML. It lets you compare two images easily. Using a range slider, you can smoothly transition between two images placed side by side. This functionality helps visualize differences or changes between images effectively.

You can use this code on websites to showcase before-and-after images. It’s handy for displaying transformations, renovations, or visual comparisons. The slider simplifies viewing changes, making it easy for visitors to see and understand differences.

How to Create Super Simple Image Comparison Slider in HTML

1. Begin by adding the HTML structure. Inside a container div with the class “compare,” create two sections, each containing an image. The images you want to compare go inside these sections. Lastly, add an input range slider with the id “range” for easy control.

<div class="compare">
  <section class="before">
    <img src="https://assets.codepen.io/2585/Runner.svg" alt="" />
  </section>
  <section class="after">
    <img src="https://assets.codepen.io/2585/Roboto.svg" alt="" />
  </section>
  <input type="range" id="range" step="0.1">
</div>

2. Apply the necessary styles using CSS. The following code takes care of the grid layout, image masking, and slider appearance. You can customize the appearance further to match your website’s design.

@layer demo {
  .compare {
    display: grid;
    
    > * {
      grid-area: 1 / 1;
    }
    
    > section {
      display: grid;
      place-content: center;
    }
    
    .before {
      -webkit-mask: linear-gradient(to right, #000 0, var(--pos, 50%), #0000 0);
              mask: linear-gradient(to right, #000 0, var(--pos, 50%), #0000 0);
    }

    .after {
      -webkit-mask: linear-gradient(to right, #0000 0, var(--pos, 50%), #000 0);
              mask: linear-gradient(to right, #0000 0, var(--pos, 50%), #000 0);
    }

    > input[type="range"] {
      z-index: 1;
      -webkit-appearance: none;
         -moz-appearance: none;
              appearance: none;
      background: transparent;
      cursor: pointer;
      -webkit-tap-highlight-color: transparent;

      &::-webkit-slider-thumb {
        -webkit-appearance: none;
                appearance: none;
        inline-size: 4px;
        block-size: 100dvh;
        background-color: CanvasText;
      } 

      &::-moz-range-thumb {
        -moz-appearance: none;
             appearance: none;
        inline-size: 4px;
        block-size: 100dvh;
        background-color: CanvasText;
      }
    }
  }
  
  img {
    max-block-size: 80dvh;
    max-inline-size: 100%;
  }
}

@layer demo.support {
  * {
    box-sizing: border-box;
    margin: 0;
  }

  html {
    block-size: 100%;
    color-scheme: dark light;
  }

  body {
    min-block-size: 100%;
    font-family: system-ui, sans-serif;
    display: grid;
  }
}

3. Finally, include the JavaScript code to enable the slider functionality. This script updates the position property (–pos) based on the slider’s input, creating a smooth transition effect between the two images.

range.oninput = () =>
document.body.style.setProperty('--pos', range.value + '%');

That’s all! hopefully, you have successfully created an Image Comparison Slider on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X