Changing Background Color with JavaScript: A Step-by-Step Guide

In this tutorial, we’ll learn how to dynamically change the background color of a webpage using JavaScript. This can be a useful feature for enhancing user experience and adding interactivity to your website.

HTML Structure:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Background Color - Wizbrand</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <h1>Change Background Color</h1>
        <button id="colorBtn">Change Color</button>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS Styling:

body {
    font-family: Arial, sans-serif;
    background-color: #f0f0f0;
    margin: 0;
    padding: 0;
}

.container {
    text-align: center;
    margin-top: 100px;
}

h1 {
    color: #333;
}

button {
    padding: 10px 20px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    outline: none;
}

button:hover {
    background-color: #0056b3;
}

JavaScript Code:

// Get the button element
const colorBtn = document.getElementById('colorBtn');

// Add click event listener to the button
colorBtn.addEventListener('click', () => {
    // Generate a random color
    const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
    
    // Change the background color of the body
    document.body.style.backgroundColor = randomColor;
});

Output:-

JavaScript code to change the background color when the button is clicked.

Hopefully, it will help you ..!!!

Related Posts

Site Reliability Engineering: Key Concepts, Tools, and Best Practices

Introduction Imagine your favorite video game crashes during a match. You feel annoyed because the game stopped working. Websites face this exact problem every single day. Millions…

Read More

The Future of Computer Operations: How Smart Systems Fix Problems

Every day, millions of people tap smartphone screens to navigate morning traffic, order groceries to their doorsteps, and transfer money between bank accounts in seconds. Behind every…

Read More

How DataOps Improves Alert Accuracy with AI Tools

Data pipelines run constantly to deliver numbers, reports, and dashboards. When something breaks, monitoring systems send alerts to data engineers. But when these notifications ring every ten…

Read More

Core Engineering Skills Needed to Master Data Pipeline Automation Systems

Introduction Imagine building a giant LEGO castle, but someone keeps swapping out your plastic bricks for blocks of melting ice. That is what working with raw digital…

Read More

Continuous Data Validation in DataOps: The Complete Architecture Guide

Continuous data validation is the systematic practice of asserting data correctness, schema consistency, and distribution integrity across every state boundary of an enterprise data pipeline. In DataOps,…

Read More

Implementing XOps: Key Pillars, Common Challenges, and Real-World Solutions

Introduction Modern engineering teams rarely run on pure application code alone. Enterprise software delivery now relies on distributed microservices, complex telemetry pipelines, machine learning inference engines, high-throughput…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x