Random Hex Generator using HTML, CSS & JavaScript

Random Hex Color Code Generator JavaScript Project

Random Hex Color Code Generator JavaScript Project

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Random Hex Generator which will randomly generate the hex code for the colors which are included in the CSS file to make the project very creative. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Random Hex Generator Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Random Hex Generator Project.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Random Hex Generator Project.

At last, we will use JS (JavaScript) which will add logic to make the Random Hex Generator Project responsive from the user end.

In this blog post, we will discuss Random Hex Color Code Generator JavaScript Project with complete source code so you can just copy and paste them into your own project. Happy exploring and learning !!

I hope you have got an idea about the project.

HTML Code for Hex Code Generator

First, we’ll start with creating the structure of the Random Hex Generator project for that as you can see in the above code we have used all the necessary elements & attributes to set up the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Hex Color Code</title>
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@600&display=swap" rel="stylesheet">
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div id="output-color">
            <span></span>
        </div>
        <input type="text" id="output" readonly>
        <div class="btns">
            <button id="gen-btn">Generate</button>
            <button id="copy-btn">Copy</button>
        </div>
    </div>
    <div class="custom-alert">Hex Code Copied</div>
    <!-- Script -->
    <script src="script.js"></script>
</body>
</html>

Google Homepage Clone Using HTML and CSS

CSS Code for Hex Code Generator

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

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    border: none;
    outline: none;
    font-family: "Roboto Mono", monospace;
}
body{
    background-color: #121317;
}
.container{
    background-color: #202229;
    width: 60vmin;
    padding: 2.5em 2em;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    font-size: 3vmin;
    border-radius: 10px;
}
#output-color{
    position: relative;
    height: 30vmin;
    width: 30vmin;
    border: 0.5em solid #f5f5f5;
    border-radius: 50%;
    margin: auto;
    display: grid;
    place-items: center;
}
#output-color span{
    display: block;
    width: calc( 100% - 20px );
    height: calc( 100% - 20px);
    border-radius: 50%;
}
.show-color{
    animation: pop 0.8s;
}
@keyframes pop{
    0%{
        transform: scale(0);
    }
    100%{
        transform: scale(1);
    }
}
input[type="text"]{
    width: 100%;
    background-color: transparent;
    border: 2.5px dashed #f5f5f5;
    font-size: 1.3em;
    padding: 0.8em 0;
    margin: 1em 0;
    border-radius: 8px;
    color: #f5f5f5;
    text-align: center;
}
input[type="text"]::-moz-selection{
    background: transparent;
}
input[type="text"]::selection{
    background: transparent;
}
.btns{
    display: flex;
    justify-content: space-around;
}
.btns button{
    font-size: 1em;
    padding: 0.8em 1.7em;
    border-radius: 7px;
    font-weight: 600;
    cursor: pointer;
}
#gen-btn{
    background-color: #18f98f;
    color: #202229;
}
#copy-btn{
    border: 3px solid #18f98f;
    background-color: transparent;
    color: #18f98f;
}
.custom-alert{
    background-color: rgba(255,255,255,0.3);
    font-size: 2.5vmin;
    padding: 1em 1.5em;
    position: fixed;
    top: 10px;
    right: 10px;
    color: #f5f5f5;
    transition: 0.5s;
    transform: translateX(calc(100% + 10px));
}

Profile ID Card Design Using HTML &CSS

JavaScript Code for Hex Code Generator

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Random Hex Generator using HTML, CSS & JavaScript (Source Code).

let outputColor = document.querySelector("#output-color span");
let output = document.getElementById("output");
let genBtn = document.getElementById("gen-btn");
let copyBtn = document.getElementById("copy-btn");
let customAlert = document.querySelector(".custom-alert");
let hexString = "0123456789abcdef";

let genHexCode = () => {
    let hexCode = "#";
    for( i = 0; i < 6; i++){
        hexCode += hexString[Math.floor(Math.random() * hexString.length)];
    }
    output.value = hexCode;
    outputColor.classList.remove("show-color");
    setTimeout( () => {
        outputColor.classList.add("show-color");
    },10);
    outputColor.style.backgroundColor = hexCode;
}

copyBtn.addEventListener("click", () => {
    output.select();
    document.execCommand("copy");
    customAlert.style.transform = "translateX(0)";
    setTimeout( () => {
        customAlert.style.transform = "translateX( calc( 100% + 10px ))";
    }, 2000);
});

window.onload = genHexCode;
genBtn.addEventListener("click", genHexCode);

Final Output

We have Successfully created our Random Hex Generator using HTML, CSS & 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.

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.

Thank You And Happy Learning!!!

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply