Skip to content

Commit ccb259b

Browse files
committed
Day 3 - CSS Variables with JS
1 parent 412b375 commit ccb259b

2 files changed

Lines changed: 43 additions & 25 deletions

File tree

03 - CSS Variables/index-START.html

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Scoped CSS Variables and JS</title>
6+
<link rel="stylesheet" href="styles.css">
67
</head>
78
<body>
89
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
@@ -20,34 +21,16 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2021

2122
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
2223

23-
<style>
24-
25-
/*
26-
misc styles, nothing to do with CSS variables
27-
*/
28-
29-
body {
30-
text-align: center;
31-
}
32-
33-
body {
34-
background: #193549;
35-
color: white;
36-
font-family: 'helvetica neue', sans-serif;
37-
font-weight: 100;
38-
font-size: 50px;
39-
}
40-
41-
.controls {
42-
margin-bottom: 50px;
43-
}
24+
<script>
25+
const inputs = document.querySelectorAll('.controls input');
4426

45-
input {
46-
width:100px;
27+
function handleUpate() {
28+
const suffix = this.dataset.sizing || '';
29+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
4730
}
48-
</style>
4931

50-
<script>
32+
inputs.forEach(input => input.addEventListener('change', handleUpate));
33+
inputs.forEach(input => input.addEventListener('mousemove', handleUpate));
5134
</script>
5235

5336
</body>

03 - CSS Variables/styles.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
:root {
2+
--base: #ffc600;
3+
--spacing: 10px;
4+
--blur: 10px;
5+
}
6+
7+
img {
8+
padding: var(--spacing);
9+
background: var(--base);
10+
filter: blur(var(--blur));
11+
}
12+
13+
.hl {
14+
color: var(--base);
15+
}
16+
17+
body {
18+
text-align: center;
19+
}
20+
21+
body {
22+
background: #193549;
23+
color: white;
24+
font-family: 'helvetica neue', sans-serif;
25+
font-weight: 100;
26+
font-size: 50px;
27+
}
28+
29+
.controls {
30+
margin-bottom: 50px;
31+
}
32+
33+
input {
34+
width:100px;
35+
}

0 commit comments

Comments
 (0)