-
Notifications
You must be signed in to change notification settings - Fork 0
/
darkmode.html
45 lines (41 loc) · 1.15 KB
/
darkmode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html>
<head>
<style>
/* Styles for light mode */
body {
background-color: #ffffff;
color: #000000;
}
/* Styles for dark mode */
.dark-mode {
background-color: #333333;
color: #ffffff;
}
</style>
</head>
<body>
<h1>Dark Mode Example</h1>
<label for="dark-mode-toggle">Dark Mode</label>
<input type="checkbox" id="dark-mode-toggle">
<script>
// Check if user has a preference for dark mode in localStorage
const isDarkMode = localStorage.getItem('darkMode');
// Apply dark mode styles if user has a preference
if (isDarkMode === 'true') {
document.body.classList.add('dark-mode');
document.getElementById('dark-mode-toggle').checked = true;
}
// Toggle dark mode on user interaction
document.getElementById('dark-mode-toggle').addEventListener('change', function() {
if (this.checked) {
document.body.classList.add('dark-mode');
localStorage.setItem('darkMode', 'true');
} else {
document.body.classList.remove('dark-mode');
localStorage.setItem('darkMode', 'false');
}
});
</script>
</body>
</html>