Image Comparison Slider Using HTML CSS and JavaScript

How to Create Image Comparison Slider Using HTML CSS and JavaScript

How to Create Image Comparison Slider Using HTML CSS and JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make an Image Comparison Slider which will help the user to compare the image with the help of an slider. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements the defined code will be an intermediate level to make Image Comparison Slider.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Image Comparison Slider project.

At last we will use JS (JavaScript) which will add a logic to make the Image Comparison Slider project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Image Comparison

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Comparison Slider</title>
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <img src="https://i.postimg.cc/mZJ7Ly7B/image-1.png">
        <img id="my-img" src="https://i.postimg.cc/W4NWvckd/image-2.png">
        <input type="range" min="0" max="100" value="50" id="slider" oninput="slide()">
    </div>


    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

First we’ll start with creating the structure of the Image Comparison Slider project for that as you can see the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

50+ Html ,Css & Javascript Projects With Source Code

CSS Code for Image Comparison

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 100vh;
    display: grid;
    place-items: center;
}
.container{
    height: 67.5vmin;
    width: 95vmin;
    position: relative;
    overflow: hidden;
}
img{
    width: 100%;
    height: 100%;
    position: absolute;
}
#my-img{
    clip-path: polygon(0 0 , 50% 0, 50% 100%, 0 100%);
}
#slider{
    position: relative;
    -webkit-appearance: none;
    width: calc( 100% + 40px);
    height: 100%;
    margin-left: -20px;
    background-color: transparent;
    outline: none;
}
#slider::-webkit-slider-thumb{
    -webkit-appearance: none;
    height: 40px;
    width: 40px;
    background: url("slider-icon.svg"),
    rgba(255,255,255,0.3);
    border: 3px solid #ffffff;
    border-radius: 50%;
    background-size: contain;
    cursor: pointer;
}

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Image Comparison Slider project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

10+ Javascript Projects For Beginners With Source Code

JavaScript Code for Image Comparison

function slide(){
    let slideValue = document.getElementById("slider").value;
    document.getElementById("my-img").style.clipPath = "polygon(0 0," + slideValue + "% 0," + slideValue + "% 100%, 0 100%)";
    console.log("polygon(0 0," + slideValue + "% 0," + slideValue + "% 100%, 0 100%)");
}

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. The condition are given how the slider will respond while moving and how much color will be visible. Let us see the Final Output of the project  Image Comparison Slider Using HTML CSS and JavaScript.

Final Output Of Image Comparison Slider Using HTML CSS and JavaScript


We have successfully created our Image Comparison Slider Using HTML CSS and JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

If you find out this Blog helpful, then make sure to search codewithrandom on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply