Skip to content

Instantly share code, notes, and snippets.

@jfeliweb
Last active March 2, 2023 14:16
Show Gist options
  • Save jfeliweb/eb759084b8a64411b83d85d8d690b85b to your computer and use it in GitHub Desktop.
Save jfeliweb/eb759084b8a64411b83d85d8d690b85b to your computer and use it in GitHub Desktop.
Adding Dark Mode for WordPress
// 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');
}
/* Dark mode styles */
body.dark-mode {
background-color: #333;
color: #fff;
}
/* Light mode styles */
body.light-mode {
background-color: #fff;
color: #333;
}
- 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