Random Color Palette Generator Using HTML, CSS and JavaScript

Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Random Color Palette Generator" project using HTML, CSS & JavaScript. 

Preview 

In the above video, you’ve seen the preview of the "Random Color Palette Generator" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS, and JavaScript code below. 

Random Color Palette Generator [Source Code] 

To get the following HTML, CSS & JS code for the Random Color Palette Generator project. You need to create three files one is an HTML file, the second one is a CSS file and another one is a JS file. After creating these three files then you can copy-paste the given codes on your document. 

Remember, you’ve to create a file with a .html extension for HTML code, a .css extension for CSS code, and .js for JavaScript code.

You can also download all source code files from the given download button.

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

<head>
    <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>Random Color Palette Generator</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>Random Color Palette Generator</h1>

    <div class="container"></div>

    <button class="refreshBtn">Refresh</button>

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

</html>
CSS
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap");

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

body {
  background-color: lightgreen;
  max-width: 1200px;
  margin: 10px auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

h1 {
  margin-bottom: 10px;
  font-weight: 600;
  text-transform: uppercase;
  word-spacing: 6px;
  letter-spacing: 1px;
}

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
}

.colorContainer {
  padding: 10px;
  background-color: white;
  border-radius: 10px;
}

.colorBox {
  width: 200px;
  height: 200px;
  background-color: black;
  border-radius: 10px;
}

.codeBox {
  font-size: 24px;
  margin-top: 10px;
  text-align: center;
}

.refreshBtn {
  width: 150px;
  height: 50px;
  margin-top: 20px;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: black;
  background-color: transparent;
  border: 3px solid black;
  border-radius: 5px;
  cursor: pointer;
}

.refreshBtn:hover {
  color: white;
  background-color: black;
}

.refreshBtn:active {
  transform: scale(0.95);
}
JavaScript
const containerEl = document.querySelector(".container");

const refreshBtnEl = document.querySelector(".refreshBtn");

const totalColorBoxes = 10; //you can can give any number of coloBoxes to generate.

function generateColorPalette(){

    // Whenever this function executes the container starts with empty color boxes.
    containerEl.innerHTML = ""; 

    // This generates totalColorBoxes 
    for (let i = 0; i < totalColorBoxes; i++) {
       
        // Random hex color code will be generated
        let hexColorCode = Math.floor(Math.random() * 0xffffff).toString(16);
        hexColorCode = `#${hexColorCode}`;
        
        // created a colorContainer in which we have added colorBox and codeBox elements.
        const colorContainerEl = document.createElement("div");
        colorContainerEl.classList.add("colorContainer");
        colorContainerEl.innerHTML = `<div class="colorBox" style="background: ${hexColorCode}"></div>
                              <p class="codeBox">${hexColorCode}</p>`;

        // It adds onClick event to every colorContainer we have created.
        colorContainerEl.addEventListener("click", () => copyColor(colorContainerEl, hexColorCode));
        
        // we are adding all the created colorContainer to container
        containerEl.appendChild(colorContainerEl);
    }
}


function copyColor(colorContainerEl, hexColorCode){
    const colorBoxEl = colorContainerEl.querySelector(".codeBox");

    // It will copy the code to clipboard or else it shows alert message.
    try{
        navigator.clipboard.writeText(hexColorCode)
        colorBoxEl.innerText = "Copied";
        setTimeout(() => {
            colorBoxEl.innerText = hexColorCode
        }, 1000)

    }catch(e){
        alert("Failed to copy the color code!");
    }
}

// At load time it automatically calls the generateColorPalette function.
generateColorPalette();

// when we click on refresh button it calls the generateColorPalette function.
generateColorPalette();
refreshBtnEl.addEventListener("click", generateColorPalette);
You have to wait 15 seconds.

Generating Download Link...

Post a Comment

Previous Post Next Post