惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
F
Full Disclosure
Recorded Future
Recorded Future
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
博客园_首页
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Hacker News: Front Page
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 司徒正美
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
Cloudbric
Cloudbric
PCI Perspectives
PCI Perspectives
有赞技术团队
有赞技术团队
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tailwind CSS Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
N
News and Events Feed by Topic
罗磊的独立博客
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
T
Tor Project blog
IT之家
IT之家
M
MIT News - Artificial intelligence
S
Security @ Cisco Blogs
O
OpenAI News
AI
AI
S
Securelist
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题

Chawye Hsu

倒带,关于虚幻勇士的记忆 - Chawye Hsu 用 Rust 写的 Scoop 再实现 - Chawye Hsu 十款好评如潮的独立游戏与十张一定要听的原声带 - Chawye Hsu 除了 GitHub 官方移动客户端,还有这些东西值得注意 - Chawye Hsu 结合响应式图片实现更好的图片懒加载方案 - Chawye Hsu 致五年 E3 青春:AMD YES! ITX 装机作业 - Chawye Hsu 35 年后,竹内玛莉亚的 Plastic Love 有了官方 MV - Chawye Hsu 总结下博客最近的调整 - Chawye Hsu 彻底搞懂 Windows 控制台的设置 - Chawye Hsu 历经数月,GitHub 章鱼猫寄来的 T 恤竟然…… - Chawye Hsu 我都在用些什么 Chrome 扩展程序 - Chawye Hsu 再谈谈 Scoop 这个 Windows 下的软件包管理器 - Chawye Hsu Microsoft Office 2016 自定义安装 - Chawye Hsu 推特背后的 Notch 之道 - Chawye Hsu Nintendo Switch 开箱与简评 - Chawye Hsu Minecraft Modding 简史 - Chawye Hsu Openwrt 配置 he.net IPv6 隧道 - Chawye Hsu 2016。2017, - Chawye Hsu .im 域名续费一记 - Chawye Hsu Newbie Mac Bootstrap - Chawye Hsu 用 Scoop 改善 Windows Powershell - Chawye Hsu Hello World - Chawye Hsu
利用 CSS 媒体查询让网页配色跟随 macOS 深色模式 - Chawye Hsu
Chawye Hsu · 2019-05-01 · via Chawye Hsu

苹果公司的操作系统 macOS 从 10.14 Mojave 版本开始,为用户提供了深色模式的可选外观。用户可以在系统中的通用设置里选择启用深色模式,整个系统的界面会全局变为暗色调。对于夜间使用场景而言,深色模式的界面可能会显得更加友好,也更能让用户集中注意力。

Office 365 Dark Mode (Twitter @derek_snook)
Office 365 Dark Mode (Twitter @derek_snook)

利用苹果公司提供的接口,各类原生桌面应用程序都开始陆续支持深色模式,可根据 macOS 用户的系统设置进行换肤,比如 Google Chrome、Microsoft Office 等。

但对于 Web 应用而言,似乎还没有什么讨论。好消息是 CSS 的媒体查询(media queries)新增加了一个叫做 prefers-color-scheme 的媒体特征,开发者可以通过该设置特征,让网站的配色随着 macOS 系统的界面外观色调改变而改变。

/* 常规浅色模式下的网页背景颜色及文本颜色 */
body {
  background: #fff;
  color: #222;
}

/* 深色模式下的网页背景颜色及文本颜色 */
@media (prefers-color-scheme: dark) {
  body {
    background-color: #222;
    color: #ddd;
  }
}

prefers-color-scheme 支持三个值,分别是 no-preferencelight、以及 dark,对应是“无指定”、“浅色主题”以及“深色主题”。详情可以查看 MDN 说明。

这个还在 CSS 草案阶段的媒体特征[1],显然尚未被主流浏览器大规模支持,不过已经开始受到部分浏览器支持了。可以从 MDN 或者 Can I Use 上看到,Firefox 67 以及 Safari 12.1 开始支持该媒体特征。

 兼容性
prefers-color-scheme 兼容性

如果你是 macOS 用户,又正巧使用的是支持该特征的浏览器的话,可以通过切换系统的外观色调,查看到我的博客配色跟随系统色调的变化。

配色跟随系统色调切换而变化
配色跟随系统色调切换而变化

除了 Firefox 浏览器之外,也可以用 macOS 系统的 Safari 查看到效果。

浅色
浅色
深色
深色

图中的文章见《总结下博客最近的调整》。不过其实 prefers-color-scheme 只是帮助做到随系统自动变化而已,网站的深浅配色系统本身还是要开发者预先设计好。


  1. https://drafts.csswg.org/mediaqueries-5/#descdef-media-prefers-color-scheme ↩︎