This lightweight (1 line JS) HTML CSS code snippet helps you to create an image comparison slider. It’s entirely built with HTML CSS and a little inline JavaScript function that dynamically updates slider values.
This comparison image slider uses CSS variables and clip-path property to compare two images. Likewise, it uses an HTML5 range slider to get the values for the clip-path. The design and layout of the slider can be easily customized by setting the custom values in CSS variables.
How to Create Image Comparison Slider in HTML CSS
1. First of all, create the HTML div element with a class name “c-compare” and place your two images inside it with class names “c-compare__left” and “c-compare__right”.
Similarly, create HTML input element with type “range” and define its class name “c-compare__range”. Inside the range input, define a JS function on the input event. The following is the complete HTML structure for the image comparison slider:
<div class="c-compare" style="--value:50%;"> <img class="c-compare__left" src="img/color.jpg" alt="Color" /> <img class="c-compare__right" src="img/bw.jpg" alt="B/W" /> <input type="range" class="c-rng c-compare__range" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', `${this.value}%`)" /> </div>
2. After that, style the image comparison slider using the following CSS. If you want to customize the slider’s thumb width, background, or hover color, you can update variable values in the “.c-compare” class.
.c-compare { --h: 9; --m: 1rem 0; --w: 16; --thumb-bgc: red; --thumb-bgc-focus: hsla(0, 70%, 70%, 0.7); --thumb-w: 1rem; margin: var(--m); position: relative; } .c-compare::after { content: ""; display: block; padding-bottom: calc((var(--h) / var(--w)) * 100%); } .c-compare__left, .c-compare__right { height: 100%; object-fit: cover; position: absolute; width: 100%; } .c-compare__left { clip-path: polygon(0% 0%, var(--value) 0%, var(--value) 100%, 0% 100%); } .c-compare__right { clip-path: polygon(100% 0%, var(--value) 0%, var(--value) 100%, 100% 100%); } .c-compare__range { background-color: transparent; box-sizing: border-box; font-family: inherit; height: 100%; margin: 0; outline: none; position: absolute; top: 0; width: 100%; } .c-compare__range::-moz-range-thumb { background-color: var(--thumb-bgc); cursor: ew-resize; height: 100%; width: var(--thumb-w); } .c-compare__range::-webkit-slider-thumb { background-color: var(--thumb-bgc); cursor: ew-resize; height: 100%; width: var(--thumb-w); } .c-compare__range:focus::-webkit-slider-thumb { background-color: var(--thumb-bgc-focus); box-shadow: 0 0 0 3px var(--thumb-bgc); } .c-compare__range:focus::-moz-range-thumb { background-color: var(--thumb-bgc-focus); box-shadow: 0 0 0 3px var(--thumb-bgc); } .c-compare__range::-moz-range-track { background: transparent; background-size: 100%; box-sizing: border-box; } .c-compare__range::-webkit-slider-runnable-track { background: transparent; background-size: 100%; box-sizing: border-box; height: 100%; } .c-compare__range, .c-compare__range::-webkit-slider-runnable-track, .c-compare__range::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; }
That’s all! hopefully, this code snippet for the “comparison image slider” is helpful for you. If you have a question or suggestion, let me know by 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.