This is a simple web application that allows users to change the background color of a box dynamically by clicking a button. The project demonstrates basic HTML, CSS, and JavaScript integration.
- Random Color Generation: Each click generates a new random color.
- Responsive Design: Works on all screen sizes.
- User-Friendly Interface: Simple and intuitive design.
- HTML: For structuring the webpage.
- CSS: For styling the elements.
- JavaScript: For adding interactivity.
The HTML file (index.html) contains the following key elements:
- A
divwith the IDcolor-boxthat represents the box whose color will change. - A
buttonwith the IDchange-color-btnthat triggers the color change.
The CSS file (styles.css) styles the elements:
- The
#color-boxis given a fixed width, height, and default background color. - The
#change-color-btnis styled to look like a button with a hover effect.
The JavaScript file (script.js) contains the logic for generating random colors and updating the box color. Here's how it works:
-
Wait for the DOM to Load: The script waits for the DOM content to be fully loaded before executing any code. This ensures that all elements are available for manipulation.
document.addEventListener("DOMContentLoaded", function () { // Code goes here });
-
Select Elements: The script selects the
color-boxandchange-color-btnelements usingdocument.getElementById().const colorBox = document.getElementById("color-box"); const changeColorBtn = document.getElementById("change-color-btn");
-
Generate Random Color: The
getRandomColor()function generates a random hexadecimal color code. It works by:- Creating a string of valid hexadecimal characters (
0123456789ABCDEF). - Looping 6 times to pick random characters and appending them to a
#symbol.
function getRandomColor() { const letters = "0123456789ABCDEF"; let color = "#"; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; }
- Creating a string of valid hexadecimal characters (
-
Add Event Listener: An event listener is added to the button. When the button is clicked, the
getRandomColor()function is called, and the background color of thecolor-boxis updated.changeColorBtn.addEventListener("click", function () { colorBox.style.backgroundColor = getRandomColor(); });
- Clone the repository or download the files.
- Open the
index.htmlfile in a web browser. - Click the "Change Color" button to see the box change color.
- Initial State: The box has a default color (e.g.,
#3498db). - After Click: The box changes to a random color (e.g.,
#e74c3c,#2ecc71,#9b59b6).
This project is open-source and available under the MIT License.
-
DOMContentLoaded Event:
- Ensures the JavaScript code runs only after the HTML is fully loaded.
- Prevents errors caused by trying to manipulate elements that don't yet exist.
-
Random Color Generation:
- Hexadecimal colors are represented as a
#followed by 6 characters (0–9, A–F). - The
getRandomColor()function builds this string by randomly selecting characters.
- Hexadecimal colors are represented as a
-
Event Handling:
- The
clickevent listener on the button triggers the color change. - The
style.backgroundColorproperty updates the box's background color.
- The
- The
Math.random()function generates a random number between 0 and 1. - Multiplying by 16 and using
Math.floor()ensures the number is an integer between 0 and 15. - The
lettersstring maps these numbers to valid hexadecimal characters.
- Add a feature to copy the generated color code to the clipboard.
- Allow users to input a specific color manually.
- Add animations for smoother color transitions.
