博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
preferreds-color-scheme:CSS媒体查询
阅读量:2523 次
发布时间:2019-05-11

本文共 3490 字,大约阅读时间需要 11 分钟。

One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result. macOS recently implemented a native dark mode but that mode doesn't convert websites to a dark interface, so you're still getting a bright site regardless of native theme. Wouldn't it be amazing if websites would also go dark or light based on user's system preference?

我逐渐欣赏的一种设备和应用功能是能够在亮模式和暗模式之间切换的功能。 如果您曾经做过深夜编码或阅读,那么您就会知道,黑暗的主题对于防止眼睛疲劳和由此引起的头痛有多么惊人。 macOS最近实现了本机暗模式,但是该模式不会将网站转换为暗界面,因此无论本机主题如何,您仍然会获得明亮的网站。 如果网站也会根据用户的系统偏好而变暗还是变亮,这会令人惊讶吗?

The CSS working group agrees, which is why they've ; a media query that signals what the user's theme preference is and allows you to code your site to match that preference!

CSS工作组对此表示同意,这就是为什么他们 ; 一个媒体查询,该查询表明用户的主题首选项是什么,并允许您对网站进行编码以匹配该主题!

The prefers-color-scheme media query has two effective values you can specify: light and dark:

prefers-color-scheme媒体查询具有两个可以指定的有效值: lightdark

/* Light mode */@media (prefers-color-scheme: light) {    html {        background: white;        color: black;    }}/* Dark mode */@media (prefers-color-scheme: dark) {    html {        background: black;        color: white;    }}

Coupled with your default site design, you could potentially offer three different designs: default (no-preference), light modifications, and dark modifications.

结合您的默认站点设计,您可能会提供三种不同的设计:默认( no-preference ),浅色修改和深色修改。

To make managing colors in each mode easier, you can simply modify CSS variables within the media query:

为了简化每种模式下的颜色管理,您只需在媒体查询中修改CSS变量即可:

/* Defaults */:root {    --color-scheme-background: pink;    --color-scheme-text-color: red;}/* Light mode */@media (prefers-color-scheme: light) {    :root {        --color-scheme-background: white;        --color-scheme-text-color: black;    }}/* Dark mode */@media (prefers-color-scheme: dark) {    :root {        --color-scheme-background: black;        --color-scheme-text-color: white;    }}/* Usage */html {    background: var(--color-scheme-background);    color: var(--color-scheme-text-color);}

If you want to use JavaScript to know which mode your user prefers, you can easily do so by :

如果您想使用JavaScript来了解用户喜欢哪种模式,则可以通过来轻松实现:

html {    content: ""; /* (ab)using the content property */}/* Light mode */@media (prefers-color-scheme: light) {    html {        content: "light"; /* (ab)using the content property */    }}/* Dark mode */@media (prefers-color-scheme: dark) {    html {        content: "dark"; /* (ab)using the content property */    }}
const mode = getComputedStyle(document.documentElement).getPropertyValue('content');// mode: "dark"

I'm pleased that there's an official media query for color/theme preference. As someone who suffers from minor headaches to skull numbing migraines, my preference is always a dark theme and I appreciate apps that put in the extra effort to provide me a painless user experience. We already use media queries to accommodate print and different viewport sizes, so let's take an extra step in providing colors based on user preference!

我很高兴有官方媒体查询颜色/主题首选项。 作为遭受轻微头痛,麻木偏头偏头痛的人,我的首选始终是黑暗主题,并且我感谢那些为我提供无痛用户体验而付出了额外努力的应用。 我们已经使用媒体查询来适应打印和不同的视口大小,因此让我们采取额外的步骤根据用户的喜好提供颜色!

Note: At the time of posting, only Safari Preview 68 has implemented this media query. Follow to know Firefox's support status.

注意:发布时,只有Safari Preview 68实施了此媒体查询。 跟随来了解Firefox的支持状态。

翻译自:

转载地址:http://pgswd.baihongyu.com/

你可能感兴趣的文章
android 开发之 - 百度地图 key 的申请
查看>>
SQL切换真假状态标识字段
查看>>
MySQL的数据类型
查看>>
获取控件在运行时于屏幕中的位置
查看>>
删除多个重复记录
查看>>
Meteor Shower POJ - 3669
查看>>
urllib
查看>>
.Net内存程序集的DUMP(ProFile篇)
查看>>
《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---40
查看>>
NIOS II 中用结构体指示灯终于正常了
查看>>
CF1009F Dominant Indices - 题解
查看>>
memached实现tomcat的session共享
查看>>
django导出excel
查看>>
阿里云服务器CentOS6.9安装maven
查看>>
【搜索】数的划分
查看>>
智能提示
查看>>
[JavaScript] 弹出编辑框
查看>>
一个消息队列MQ调试工具
查看>>
springmvc 访问时找不到配置文件
查看>>
采访吴岳师兄有感 by 王宇飞
查看>>