Color Slider using HTML, CSS & JavaScript

Color Slider using HTML, CSS & JavaScript (RGB Color Slider)

Color Slider using HTML, CSS & JavaScript (RGB Color Slider)

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make color slider which will provide different shades of the colors with appropriate value using RGB. 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. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment. At last we will use JS (JavaScript) which will add a logic to make the project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Slider

<html lang="en">
<head>
    <title>RGB Color Slider</title>
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Work+Sans:wght@500&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
   <div class="container">
       <div class="wrapper">
           R<input type="range" min="0" max="255" value="0" id="red" oninput="colors()">
       </div>
       <div class="wrapper">
            G<input type="range" min="0" max="255" value="0" id="green" oninput="colors()">
        </div>
        <div class="wrapper">
            B<input type="range" min="0" max="255" value="0" id="blue" oninput="colors()">
        </div>
        <span id="output">rgb(0, 0, 0)</span>
   </div>
</body>
</html>

First we’ll start with creating the structure of the 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.

CSS Code for Slider

body{
    background-color: #000000;
}
.container{
    background-color: #ffffff;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    width: 60%;
    padding: 20px 0;
    box-shadow: 20px 20px 30px rgba(0,0,0,0.2);
    font-family: 'Work Sans',sans-serif;
    font-size: 20px;
    font-weight: 500;
    color: #142b37;
    border-radius: 5px;
    display: grid;
    justify-items: center;
}
.wrapper{
    width: 100%;
    text-align: center;
}
input[type="range"]{
    display: inline-block;
    width: 80%;
    margin-left: 10px;
    margin-top: 25px;
}
span{
    display: inline-block;
    text-align: center;
    margin: 20px 0;
    background-color: #0075FF;
    color: #ffffff;
    padding: 10px 30px;
    font-size: 18px;
    letter-spacing: 0.5px;
    border-radius: 2px;
}

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the 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.

JavaScript Code for Slider

function colors(){ var red= document.getElementById("red").value; var green = document.getElementById("green").value; var blue = document.getElementById("blue").value; document.body.style.backgroundColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; document.getElementById("output").innerHTML = 'rgb(' + red + ',' + green + ',' + blue + ')' ; }

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement. Let us see the Final Output of the project Color Slider using HTML, CSS & JavaScript | RGB Color Slider

Final Output


We have Successfully created our Color Slider using HTML, CSS & JavaScript | RGB Color Slider. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random 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