Skip to content

Instantly share code, notes, and snippets.

@SparkySimp
Created February 15, 2024 18:55
Show Gist options
  • Save SparkySimp/1add7f802b520897dbb9c44c4db49187 to your computer and use it in GitHub Desktop.
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
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