Last active
March 2, 2023 14:16
-
-
Save jfeliweb/eb759084b8a64411b83d85d8d690b85b to your computer and use it in GitHub Desktop.
Adding Dark Mode for WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Detect user's color preference and load appropriate style sheet | |
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | |
document.documentElement.classList.add('dark-mode'); | |
} else { | |
document.documentElement.classList.add('light-mode'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dark mode styles */ | |
body.dark-mode { | |
background-color: #333; | |
color: #fff; | |
} | |
/* Light mode styles */ | |
body.light-mode { | |
background-color: #fff; | |
color: #333; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Create a new style sheet that includes your dark mode styles (e.g., background color, text color, etc.) | |
- Use JavaScript to detect whether the user prefers a dark or light color scheme, and load the appropriate style sheet accordingly | |
- Use the prefers-color-scheme media query to detect the user's color preference. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment