<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Color Changer</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Background Color Changer</h1>
<button id="changeColorBtn">Change Color</button>
<script>
// Selecting the button element
const changeColorBtn = document.getElementById('changeColorBtn');
// Adding an event listener to the button
changeColorBtn.addEventListener('click', function() {
// Generating a random color
const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
// Changing the background color of the body
document.body.style.backgroundColor = randomColor;
// Logging the color change
console.log('Background color changed to: ' + randomColor);
});
</script>
</body>
</html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter