You are currently viewing Change Background Color Using HTML, CSS, & JavaScript

Change Background Color Using HTML, CSS, & JavaScript

Change Background Color Using HTML, CSS, & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a Change Background Color Using HTML, CSS & JavaScript of the project screen randomly as the user wants.

In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project. The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements.

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

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

In this blog post, we will discuss how to Change Background Color Using HTML, CSS & JavaScript with complete source code for you so that you can just copy and paste it into your own project. Happy exploring and learning !!

HTML Code for Change Background Color

<html>
<head>
    <title>Random Background Color</title>
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
    <!--Script-->
    <script src="script.js"></script>
</head>
<body>
    <button onclick="changeColor()">CLICK ME</button>
</body>
</html>

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

Image Checkbox Using HTML and CSS

CSS Code for Change Background Color

*{
    padding: 0;
    margin: 0;
}
body{
    background-color: #283faa;
    height: 100vh;
    display: grid;
    place-items: center;
}
button{
    font-family: 'Poppins',sans-serif;
    font-size: 20px;
    padding: 20px 45px;
    background-color: transparent;
    border: 5px solid #ffffff;
    color: #ffffff;
    letter-spacing: 1.5px;
    border-radius: 50px;
    cursor: pointer;
    outline: none;
}

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

Text To Speech Converter Using HTM, CSS & Javascript

JavaScript Code for Change Background Color

function changeColor(){
    document.body.style.backgroundColor=
    'rgb(' + Math.round(Math.random() * 255) +
    ',' +  Math.round(Math.random() * 255) +
    ',' + Math.round(Math.random() * 255) + ')';
}

Last stage of the project the JavaScript in which we added the logic and coded as per the requirement also we have defined a function that will randomly display the color in the background of the project. Let us see the Final Output of the project Background Color Changer using HTML, CSS & JavaScript.

Final Output


We have successfully created our Change Background Color 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 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!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply