Skip to content

Commit f288fd1

Browse files
feat: add Random Color Generator mini project
1 parent 84b49f6 commit f288fd1

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

random-color-generator/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Random Color Generator</title>
6+
<link rel="stylesheet" href="styles.css">
7+
</head>
8+
<body>
9+
<div class="container">
10+
<div id="colorCode">#FFFFFF</div>
11+
<button id="btn">Generate Color</button>
12+
</div>
13+
14+
<script src="script.js"></script>
15+
</body>
16+
</html>

random-color-generator/script.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const colorCode = document.getElementById('colorCode');
2+
const btn = document.getElementById('btn');
3+
4+
function getRandomColor() {
5+
const hex = Math.floor(Math.random() * 0xffffff)
6+
.toString(16)
7+
.padStart(6, '0');
8+
return `#${hex}`;
9+
}
10+
11+
function setColor() {
12+
const color = getRandomColor();
13+
document.body.style.backgroundColor = color;
14+
colorCode.textContent = color;
15+
}
16+
17+
btn.addEventListener('click', setColor);
18+
window.addEventListener('load', setColor);

random-color-generator/styles.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
body, html {
5+
margin: 0;
6+
height: 100%;
7+
display: flex;
8+
justify-content: center;
9+
align-items: center;
10+
font-family: sans-serif;
11+
background-color: #ffffff;
12+
}
13+
.container {
14+
text-align: center;
15+
padding: 2rem;
16+
border: 2px solid #ccc;
17+
border-radius: 12px;
18+
background-color: white;
19+
}
20+
#colorCode {
21+
font-size: 2rem;
22+
margin-bottom: 1rem;
23+
font-weight: bold;
24+
}
25+
#btn {
26+
padding: 10px 20px;
27+
font-size: 1rem;
28+
border: none;
29+
background-color: #0077ff;
30+
color: white;
31+
border-radius: 6px;
32+
cursor: pointer;
33+
}
34+
#btn:hover {
35+
background-color: #005fcc;
36+
}

0 commit comments

Comments
 (0)