Random Gradient Generator With CSS & JavaScript

Random Gradient Generator With CSS & JavaScript

Random Gradient Generator With CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a Random Gradient Generator which will randomly generate which is basically a generator for the webpage background in which a color is lighter to darker in a diagonal path. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Random Gradient 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 Gradient 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 Gradient Generator Project.

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

In this blog post, we will discuss Gradient Generator Using CSS & JavaScript 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 Random Gradient Generator

First, we’ll start with creating the structure of the Random Gradient 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 Gradient Generator</title>
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap" rel="stylesheet">
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="wrapper">
        <div id="output-color"></div>
        <input type="text" id="output-code" readonly>
        <div class="btn-container">
            <button id="generate-btn">Generate</button>
            <button id="copy-btn">Copy</button>
        </div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
</body>
</html>

Sticky Footer At Bottom using HTML & CSS

CSS Code for Random Gradient Generator

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the Random Gradient Generator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    border: none;
    outline: none;
    font-family: "Poppins",sans-serif;
}
body{
    height: 100vh;
    background: linear-gradient(
        #f7f9fd 50%,
        #587ef4 50%
    );
}
.wrapper{
    width: 80vmin;
    background-color: #ffffff;
    padding: 50px 30px;
    border-radius: 8px;
    position: absolute;
    transform: translate(-50%,-50%);
    left: 50%;
    top: 50%;
    box-shadow: 0 20px 25px rgba(60,60,100,0.15);
}
#output-color{
    width: 100%;
    height: 35vmin;
    border-radius: 5px;
}
#output-code{
    background-color: #f1f5fc;
    font-size: 2.3vmin;
    font-weight: 400;
    color: #3f415f;
    width: 100%;
    padding: 15px 10px;
    border-radius: 5px;
    margin: 20px 0 40px 0;
}
.btn-container{
    display: flex;
    justify-content: space-around;
}
.btn-container button{
    background-color: #587ef4;
    min-width: 40%;
    padding: 15px 0;
    color: #ffffff;
    border-radius: 30px;
    font-size: 2.6vmin;
    font-weight: 500;
    cursor: pointer;
}

Create Random Joke Generator In HTML, CSS &JavaScript

JavaScript Code for Random Gradient 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 Gradient Generator using HTML, CSS & JavaScript.

let generateBtn = document.getElementById("generate-btn");
let copyBtn = document.getElementById("copy-btn");
let outputColor = document.getElementById("output-color");
let outputCode = document.getElementById("output-code");
let hexString = "0123456789abcdef";

let randomColor = () => {
    let hexCode = "#";
    for( i=0; i<6; i++){
        hexCode += hexString[Math.floor(Math.random() * hexString.length)];
    }
    return hexCode;
}

let generateGrad = () => {
    let colorOne = randomColor();
    let colorTwo = randomColor();
    let angle = Math.floor(Math.random() * 360);
    outputColor.style.background = `linear-gradient(${angle}deg, ${colorOne}, ${colorTwo})`;
    outputCode.value = `background: linear-gradient(${angle}deg, ${colorOne}, ${colorTwo});`;
}

copyBtn.addEventListener("click", () => {
    outputCode.select();
    document.execCommand("copy");
    alert("Code Copied To Clipboard");
});

generateBtn.addEventListener("click", generateGrad);
window.onload = generateGrad;

Final Output

We have successfully created our Random Gradient 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 codewithrandom on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You For Visiting!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply