Gradient Color Generator using HTML, CSS and JavaScript – Coding Torque

Share your love

Welcome to Coding Torque. In this blog, we are going to create a Gradient Color Generator using HTML, CSS and JavaScript. You should create this project if you are a beginner and learning JavaScript. In this project, two color inputs are grabbed on changed using javascript and the gradient is generated.

Before we start, here are some JavaScript Games you might like to create:

1.Ā Snake Game using JavaScript

2.Ā 2D Bouncing Ball Game using JavaScript

3.Ā Rock Paper Scissor Game using JavaScript

4.Ā Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript


I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.



Starter Template

<!doctype html>
<html lang="en">

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href=""
        crossorigin="anonymous" />

    <!-- CSS -->
    <link rel="stylesheet" href="style.css">

    <title>Gradient Color Generator using JavaScript with Source Code - @code.scientist x @codingtorque</title>

    <!-- Further Code here -->    

    <script src="script.js"></script>


Paste the below code in your <body> tag

<div class="container">
    <h3 style="text-align: center;">Gradient Generator</h3>
    <div id="gradient-box"></div>
    <div class="copycode">
        <input type="text" id="color-code" value="linear-gradient(45deg, deepskyblue, blue)" readonly />
        <button title="click to copy" id="copyBtn"><i class="fas fa-copy"></i></button>
        <input type="color" name="color1" id="color1" value="#ffffff">
        <input type="color" name="color2" id="color2" value="#ffffff">

Output Till Now

CSS CodeĀ 

Create a fileĀ style.css and paste the code below.

@import url("");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #5181ff;

.container {
  margin-top: 10rem;
  background-color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 20rem;
  padding: 26px;
  border-radius: 10px;

#gradient-box {
  height: 8rem;
  width: 16rem;
  margin: 1rem 0;
  border-radius: 10px;
  background: linear-gradient(45deg, deepskyblue, blue);

.copycode {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  background-color: #f1f5fc;
  border-radius: 5px;
  border: 1px solid #cfd5d5;
  margin-bottom: 10px;

#color-code {
  width: 100%;
  color: #044db4;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 10px 10px;
  background: transparent;
  border: none;
  outline: none;

#copyBtn {
  width: 30px;
  height: 100%;
  background-color: transparent;
  border: none;
  cursor: pointer;
  font-size: 15px;

Output Till Now

gradient color generator using javascript with source code

JavaScript CodeĀ 

Create a fileĀ script.jsĀ and paste the code below.
let color1 = document.getElementById("color1");
let color2 = document.getElementById("color2");
let gradient_box = document.getElementById("gradient-box");
let color_code = document.getElementById("color-code");

function changeGradient() { = `linear-gradient(45deg, ${color1.value}, ${color2.value})`;
    color_code.value = `${} ;`

color1.addEventListener("input", changeGradient);
color2.addEventListener("input", changeGradient);
Written by: Piyush Patil
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpfulšŸ’–
Share your love