How to Change Background Color using HTML, CSS and Javascript?

TechLoons
Nov 8, 2023

--

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change BG Color</title>

<style>
.container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}

.btn {
border: 0;
outline: 0;
border-radius: 4px;
padding: 5px 15px;
margin: 8px;
background: #333;
font-weight: bold;
}

.red {
color: red;
}
.yellow {
color: yellow;
}
.teal {
color: teal;
}
</style>
</head>
<body>
<div class="container">
<button class="btn red" id="btn" onclick="redBtn()">Red</button>
<button class="btn yellow" id="btn" onclick="yellowBtn()">Yellow</button>
<button class="btn teal" id="btn" onclick="tealBtn()">Teal</button>
</div>


<script>
function redBtn() {
document.body.style.backgroundColor = 'red';
}

function yellowBtn() {
document.body.style.backgroundColor = 'yellow';
}

function tealBtn() {
document.body.style.backgroundColor = 'teal';
}
</script>
</body>
</html>

--

--

TechLoons
TechLoons

Written by TechLoons

Welcome to TechLoons, your go-to source for the latest tips and information on a wide range of topics.