使“夜间模式”最有效的方法是什么?在CSS中?

发布于 2025-01-10 09:56:19 字数 1436 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再见回来 2025-01-17 09:56:19

最好的方法可能是使用 CSS 中的 prefers-color-scheme 媒体功能。

示例来自
https://developer.mozilla.org /en-US/docs/Web/CSS/@media/prefers-color-scheme

.day   { background: #eee; color: black; }
.night { background: #333; color: white; }

@media (prefers-color-scheme: dark) {
  .day.dark-scheme   { background:  #333; color: white; }
  .night.dark-scheme { background: black; color:  #ddd; }
}

@media (prefers-color-scheme: light) {
  .day.light-scheme   { background: white; color:  #555; }
  .night.light-scheme { background:  #eee; color: black; }
}

.day, .night {
  display: inline-block;
  padding: 1em;
  width: 7em;
  height: 2em;
  vertical-align: middle;
}
<div class="day">Day (initial)</div>
<div class="day light-scheme">Day (changes in light scheme)</div>
<div class="day dark-scheme">Day (changes in dark scheme)</div> <br>

<div class="night">Night (initial)</div>
<div class="night light-scheme">Night (changes in light scheme)</div>
<div class="night dark-scheme">Night (changes in dark scheme)</div>

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

.day   { background: #eee; color: black; }
.night { background: #333; color: white; }

@media (prefers-color-scheme: dark) {
  .day.dark-scheme   { background:  #333; color: white; }
  .night.dark-scheme { background: black; color:  #ddd; }
}

@media (prefers-color-scheme: light) {
  .day.light-scheme   { background: white; color:  #555; }
  .night.light-scheme { background:  #eee; color: black; }
}

.day, .night {
  display: inline-block;
  padding: 1em;
  width: 7em;
  height: 2em;
  vertical-align: middle;
}
<div class="day">Day (initial)</div>
<div class="day light-scheme">Day (changes in light scheme)</div>
<div class="day dark-scheme">Day (changes in dark scheme)</div> <br>

<div class="night">Night (initial)</div>
<div class="night light-scheme">Night (changes in light scheme)</div>
<div class="night dark-scheme">Night (changes in dark scheme)</div>

剑心龙吟 2025-01-17 09:56:19

您可以使用内置的prefers-color-scheme媒体功能
它将与用户操作系统设置(例如浅色或深色模式)或用户代理设置一起使用。
了解有关该主题的更多信息

这是一个关于如何为深色模式创建自定义样式的简单示例:

body {
  color: #000;
  background: #fff;
}

@media (prefers-color-scheme: dark) {
  body {
    color: #fff;
    background: #000;
  }
}
<p>
This is a simple paragraph
</p>

或者,您可以执行自定义样式,而不依赖于用户系统首选项,如下例所示:

function toggleDarkMode() {
  document.body.classList.toggle('dark');
}
body {
  background: white;
  color: black;
}

.dark {
  background: black;
  color: white;
}
<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');
}
body {
  background: white;
  color: black;
}

.dark {
  background: black;
  color: white;
}
<p>
This is an example paragraph
</p>
<button onclick="toggleDarkMode()">Toggle dark mode</button>

It can be improved by storing the dark mode/light mode to the localStorage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文