<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Text Color</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1 id="text">Change My Color!</h1>
<button id="colorButton">Change Color</button>
<script src="script.js"></script>
</body>
</html>
CSS
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
h1 {
transition: color 0.3s; /* Smooth transition for color change */
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
JS
const button = document.getElementById('colorButton');
const text = document.getElementById('text');
button.addEventListener('click', () => {
// Generate a random color
const randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
text.style .color = randomColor;
});