Created
February 15, 2024 18:55
-
-
Save SparkySimp/1add7f802b520897dbb9c44c4db49187 to your computer and use it in GitHub Desktop.
Automatic application of the Boootstrap dark and light color schemes in a responsive manner
This file contains 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
document.addEventListener('DOMContentLoaded', (event) => { | |
if (window.matchMedia('(prefers-color-scheme: light)').matches) { | |
document.documentElement.setAttribute("data-bs-theme", "light"); | |
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) { | |
document.documentElement.setAttribute("data-bs-theme", "dark"); | |
} | |
}); | |
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', event => { | |
if (event.matches) { | |
document.documentElement.setAttribute("data-bs-theme", "light"); | |
} | |
}); | |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { | |
if (event.matches) { | |
document.documentElement.setAttribute("data-bs-theme", "dark"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment