HTML CSS Image Comparison Slider

HTML CSS Image Comparison Slider
Code Snippet:Accessible Image Compare - 1 line JS
Author: Mads Stoumann
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 9,380
License: MIT
Edit Code online: View on CodePen
Read More

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.

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