Learn the Random Gradient Generator in HTML, CSS and Javascript - Step by Step


Welcome to this relatively simple and fun project that will use HTML, CSS Javascript to create a Webpage that will randomly generate a gradient background


Click here to see what it will look like


Part 1 - Set up a basic HTML page

<!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8">   <meta name="viewport" content="width=device-width, initial-scale=1.0">   <title>My Gradient Page</title> </head> <body>   <h1>Welcome to my Gadient Generator</h1> </body> </html>

So what is actually going on?

It's pretty simple really, you surround the information you want with tags and every opening tag has a closing one

Click here to see what it will look like



Part 2 - Let's start with a simple HTML setup for the Gradient Page


    <div class="container">
     <div class="hex-colors">
      <h1>Begin learning - Click the button below to randomise the hex colours</h1>
      <h2>The hex code of the colours are: <span id="hex-code">#000000 , #000000</span></h2>
      <p> </p>
      <button onclick="changeColor()" class="btn btn-primary">Click Me</button>
      <p> </p>
      <p> </p>
      <p> </p>
      <p><a href="learngradient.html">Click here to learn</a></p>
     </div>
    </div>

So what is actually going on?

There are a couple of things we will ignore for now such as the #000000 hex numbers and the changecolor() reference


<Div> <Span> <P> <A Href> <Button>
A div is a section of a page that can be named and we can later give it some rules on how to bahave (size, colout, etc) A span is just a section of the page we can give rules to (like a div but smaller) A new paragraph so not everything is on the same line a is the HTML for a link and href is the reference of where the link is to go - a hyperlink A standard page button that can trigger an event - this will be used to trigger the gradient change

Click here to see what it will look like



Part 3 - Let's add some basic bootstrap styling



<link rel="stylesheet" href= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

So what is actually going on?

Here we are just linking some styling rules to make make the font and button look slightly different. This comes from a bootstrap library which is a bunch of preset rules and stylings



Click here to see what it will look like


Part 4 - Let's add our own styling

    <STYLE>
    @import url('https://fonts.googleapis.com/css2?family=Comfortaa:wght@500&display=swap');
                    
    body {
        font-family: 'Comfortaa', cursive;
    }
    
    a {
        text-decoration: none;
    }
                    
    a:hover {
        text-decoration: underline;
        font-weight: bold;
    }
                    
    .container {
        width: 95%;
        margin: auto;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
                    
    .hex-colors {
        width: 100%;
        text-align: center;
    }
                    
    
    .btn-primary {
        display: block;
        height: 50px;
        width: 150px;
        border-radius: 5%;
        margin: 0 auto;
        background-color: #333333;
        border: none;
        transition: all 1s ease;
        transform: scale(1);
    }
                    
    .btn-primary:hover {
        background-color: #111111;
        transform: scale(1.05) perspective(1px);
    }
                    
    .hex-colors h1 {
        text-transform: uppercase;
        font-size: 2rem;
        /*animation: colorchange 5s infinite alternate; */
    }
                    
    .hex-colors h2 {
        margin-top: 15%;
        font-size: 2rem;
    }
                    
    @keyframes colorchange {
        0% {
            color: indigo;
        }
        20% {
            color: blue;
        }
        40% {
            color: green;
        }
        60% {
            color: yellow;
        }
        80% {
            color: orange;
        }
        100% {
            color: red;
        }
        }
                    
    @media (max-width: 480px) {
        .hex-colors h1 {
            font-size: 1.5rem;
        }
        .hex-colors h2 {
            font-size: 1.5rem;
        }
    }
    </STYLE>

                            

So what is actually going on?

Let's break down some of the sections:



Click here to see what it will look like


Now the page looks great apart from the fact that it doesn't actually do anything. Now we will give it some script but first you need to know a bit about hex colours:


Part 5 - Let's give it some functionality with some javascript

                
<script>
    function changeColor() {
        var hex_numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
    
        var hexcode = '';
        var hexcode2 = '';
    
        for (var i = 0; i < 6; i++) {
            var random_index = Math.floor(Math.random() * hex_numbers.length);
            hexcode += hex_numbers[random_index];
            var random_index2 = Math.floor(Math.random() * hex_numbers.length);
            hexcode2 += hex_numbers[random_index2];
            var startval1 = "#" + hexcode;
            var startval2 = "#" + hexcode2;
        }
    
        document.getElementById("hex-code").innerHTML = '#' + hexcode + ', #' + hexcode2;
        document.body.style.backgroundImage = "linear-gradient(to right, " + startval1 + ", " + startval2 + ")";
    }
</script>
                
            

So what is actually going on?

Let's break down some of the sections:


var hex_numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];

This creates a list of all possible hex digits that we can rondomly reference


function changecolor() var hexcode = '' for (var i = 0; i < 6; i++)
A function is a section of code that does a job. We only have one function that is called by the botton in the HTML This is a variable that holds a 6 digit hexcode (we need two of these) This is a loop. it loops 6 times and we use the counter 'i' to reference a hex number in the 6 digit sequaence

var random_index = Math.floor
(Math.random() * hex_numbers.length);
var startval1 = "#" + hexcode; document.body.style
.backgroundImage =
"linear-gradient(to right, " + startval1
+ ", " + startval2 + ")";
This calls in the random number generator to generate a number to access the list of hex numbers now we assign the hexcode with the # to create a code to reference Now we can use the two hex numbers to create the random gradient and aply it to the body of the document

Click here to see what it will look like