<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:[email protected]&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>
*{
    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));
}
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);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.