Create a Dynamic Gradient Color Generator with HTML, CSS, and JavaScript
Tags: html, css, javascript, incoderweb
Published: 2023-04-22 06:21:19

Hello friends, today in this blog, we will learn how to create a dynamic gradient color generator with HTML, CSS, and JavaScript. In our previous blog, we saw [how to build a custom QR Code Generator with qrcode.js and JavaScript](https://incoderweb.blogspot.com/2023/04/qr-code-generator.html). You can check my other [javascript projects](https://incoderweb.blogspot.com/search/label/Javascript) after reading this blog.

Do you love experimenting with color combinations in your web design projects? Are you looking for a fun and creative way to practice your HTML, CSS, and JavaScript skills? If so, you'll definitely want to check out the dynamic gradient color generator we'll be building in this blog post!

A gradient color generator allows you to create unique color combinations that transition smoothly from one hue to another. By using HTML, CSS, and JavaScript, we can build a custom color generator that lets you create your own gradient color schemes with just a few clicks. Whether you're a beginner or an experienced web developer, this project is a great way to learn new skills and have some fun in the process.

In this blog post, we'll take a deep dive into building a dynamic gradient color generator from scratch using HTML, CSS, and JavaScript. We'll cover the tools and technologies we'll be using, walk through the steps involved in building the generator, and provide some tips for customizing it to fit your needs. By the end of this post, you'll have a fully functional color generator that you can use in your own web design projects. So, let's get started!

> Note:
You can read full article, Check Live Demo of this project and download files from [here](https://incoderweb.blogspot.com/2023/04/%20Gradient-Color-Generator.html).

## Code of HTML, CSS, and JavaScript Files
Here's the good news: you don't have to write all the code of this project from scratch! I have created a [GitHub repository](https://github.com/InCoderWeb/gradientColorGenerator) that contains all the HTML, CSS, and JavaScript code needed to build the app. You can check it out and use it as a starting point for your own project.

## HTML Code

```
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- --------------------- Created By InCoder --------------------- -->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gradient Color Generator - InCoderWeb</title>
    <link rel="stylesheet" href="main.css">
</head>

<body>
    <div class="mainContainer">
        <div class="gradientOutput"></div>
        <div class="colorOptions">
            <div class="colorDirection">
                <p style="font-size: .9rem; margin-bottom: .3rem;">Colors Direction</p>
                <select name="colorDirection" id="colorDirectionSelect">
                    <option value="to top">Top</option>
                    <option value="to top right" selected>Top Right</option>
                    <option value="to right">Right</option>
                    <option value="to bottom right">Bottom Right</option>
                    <option value="to bottom">Bottom</option>
                    <option value="to bottom left">Bottom Left</option>
                    <option value="to left">Left</option>
                    <option value="to top left">Top Left</option>
                </select>
            </div>
            <div class="colorInputs">
                <p style="font-size: .9rem;">Colors</p>
                <input type="color" value="#000000" class="colorInput">
                <input type="color" value="#ff0000" class="colorInput">
            </div>
        </div>
        <textarea id="gradientCode" disabled>background: linear-gradient(#000, red);</textarea>
        <div class="optionButtons">
            <button id="randomColor">Random Color</button>
            <button id="copyBtn">Copy Code</button>
        </div>
    </div>

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

</html>
```

## CSS Code

```
@import url("https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap");
/* --------------------- Created By InCoder --------------------- */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Ubuntu", sans-serif;
}

body {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to top right, #834aaa, #7c7cd9);
}

.mainContainer {
  width: 20rem;
  display: flex;
  align-items: center;
  border-radius: 1rem;
  flex-direction: column;
  justify-content: start;
  background-color: #fff;
}

.gradientOutput {
  width: 90%;
  height: 12rem;
  margin-top: 1rem;
  margin-left: auto;
  margin-right: auto;
  border-radius: 1rem;
}

.colorOptions {
  width: 100%;
  display: flex;
  margin-top: 1rem;
  align-items: center;
  justify-content: space-around;
}

.colorDirection select {
  width: 9rem;
  height: 2rem;
  padding: 0.2rem;
  padding-right: 0.2rem;
  border-radius: 0.5rem;
  border: 2px solid #00000040;
}

.colorDirection select option {
  cursor: pointer;
}

.colorDirection select:focus {
  outline: none;
  border: none;
  border: 2px solid #0000008c;
}

.colorInput {
  border: none;
  width: 3.5rem;
  height: 2.5rem;
  cursor: pointer;
  appearance: none;
  border-radius: 0.5rem;
  -moz-appearance: none;
  -webkit-appearance: none;
  background-color: transparent;
}
.colorInput::-webkit-color-swatch {
  border: none;
  border-radius: 0.3rem;
}
.colorInput::-moz-color-swatch {
  border: none;
  border-radius: 0.3rem;
}

#gradientCode {
  width: 90%;
  height: 3.2rem;
  resize: none;
  padding: 0.5rem;
  margin-top: 1rem;
  border-radius: 0.5rem;
  border: 2px solid #00000047;
  -webkit-user-select: none; /* Safari */
  -ms-user-select: none; /* IE 10 and IE 11 */
  user-select: none; /* Standard syntax */
}

.optionButtons {
  width: 90%;
  display: flex;
  margin-top: 1rem;
  margin-bottom: 1rem;
  justify-content: space-between;
}

.optionButtons button {
  border: 0px;
  z-index: 1;
  width: 8.5rem;
  color: #fff;
  height: 2.5rem;
  overflow: hidden;
  font-size: 1rem;
  cursor: pointer;
  position: relative;
  border-radius: 0.3rem;
  text-decoration: none;
  background-position: 5rem;
  text-transform: capitalize;
  background: linear-gradient(to top right, #834aaa, #7c7cd9);
}

.optionButtons button::before {
  content: "";
  top: 0%;
  left: -100%;
  z-index: -1;
  width: 100%;
  height: 100%;
  color: #fff;
  position: absolute;
  transition: left 0.4s;
  background-color: #202020;
}

.optionButtons button:hover::before {
  left: 0%;
}

#copyBtn {
  background: #202020!important;
}

#copyBtn::before {
  background: linear-gradient(to top right, #834aaa, #7c7cd9)!important;
}
```

## JavaScript Code

```
// --------------------- Created By InCoder ---------------------
const gradientOutput = document.querySelector('.gradientOutput')
colorDirectionSelect = document.querySelector('#colorDirectionSelect')
gradientCode = document.getElementById('gradientCode')
randomColor = document.getElementById('randomColor')
copyBtn = document.getElementById('copyBtn')
colorInput = document.querySelectorAll('.colorInput')

const generateRandomColor = () => {
    let colorCode = `#${Math.floor(Math.random() * 16777215).toString(16)}`;
    return colorCode
}

let color1 = generateRandomColor()
let color2 = generateRandomColor()
let cssCode = `background: linear-gradient(${colorDirectionSelect.value}, ${color1}, ${color2});`
colorInput[0].value = color1
colorInput[1].value = color2
gradientOutput.setAttribute('style', cssCode)
gradientCode.value = cssCode

colorDirectionSelect.addEventListener('change', (e) => {
    cssCode = `background: linear-gradient(${e.target.value}, ${colorInput[0].value}, ${colorInput[1].value});`
    gradientOutput.setAttribute('style', cssCode)
    gradientCode.value = cssCode
})

colorInput.forEach(input => {
    input.addEventListener('input', (e) => {
        cssCode = `background: linear-gradient(${colorDirectionSelect.value}, ${colorInput[0].value}, ${colorInput[1].value});`
        gradientOutput.setAttribute('style', cssCode)
        gradientCode.value = cssCode
    })
});


randomColor.addEventListener('click', () => {
    colorInput[0].value = generateRandomColor()
    colorInput[1].value = generateRandomColor()
    cssCode = `background: linear-gradient(${colorDirectionSelect.value}, ${colorInput[0].value}, ${colorInput[1].value});`
    gradientOutput.setAttribute('style', cssCode)
    gradientCode.value = cssCode
})

copyBtn.addEventListener('click', () => {
    navigator.clipboard.writeText(cssCode).then(function () {
        copyBtn.innerHTML = "Code Copied"
        setTimeout(() => {
            copyBtn.innerHTML = "Copy Code"
        }, 1000)
    }, function (err) {
        console.log('Not Copied')
    });
})
```