<p>
This is an example paragraph
</p>
<button onclick="toggleDarkMode()">Toggle dark mode</button>
可以通过将深色模式/浅色模式存储到localStorage来改进
You can use the built in prefers-color-scheme media feature It will goes with the user operating system setting (e.g. light or dark mode) or a user agent setting. read more about the topic
Here's a simple example on how to create custom style for dark mode:
body {
color: #000;
background: #fff;
}
@media (prefers-color-scheme: dark) {
body {
color: #fff;
background: #000;
}
}
<p>
This is a simple paragraph
</p>
Or you can do a custom styling without relaying on the user system preferences like the example below :
function toggleDarkMode() {
document.body.classList.toggle('dark');
}
发布评论
评论(2)
最好的方法可能是使用 CSS 中的
prefers-color-scheme
媒体功能。示例来自
https://developer.mozilla.org /en-US/docs/Web/CSS/@media/prefers-color-scheme
The best way is probably to use the
prefers-color-scheme
media feature in CSS.Example from
https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme
您可以使用内置的
prefers-color-scheme
媒体功能它将与用户操作系统设置(例如浅色或深色模式)或用户代理设置一起使用。
了解有关该主题的更多信息
这是一个关于如何为深色模式创建自定义样式的简单示例:
或者,您可以执行自定义样式,而不依赖于用户系统首选项,如下例所示:
可以通过将深色模式/浅色模式存储到localStorage来改进
You can use the built in
prefers-color-scheme
media featureIt will goes with the user operating system setting (e.g. light or dark mode) or a user agent setting.
read more about the topic
Here's a simple example on how to create custom style for dark mode:
Or you can do a custom styling without relaying on the user system preferences like the example below :
It can be improved by storing the dark mode/light mode to the localStorage