if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.getElementsByTagName("body")[0].classList.add("dark");
}
In the above code example, we are checking the dark mode using window.matchMedia and assigning a class to the body tag, and this way, you can make the content inside it in dark theme.
You can set the body background of the document using CSS.
body {
background: #000000;
color: #fff;
}
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.getElementsByTagName("body")[0].setAttribute("data-theme", "dark");
}
0 Comments