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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.