首页 > 其他分享 >perfers-color-scheme 使用简单介绍

perfers-color-scheme 使用简单介绍

时间:2024-06-05 13:11:01浏览次数:12  
标签:操作系统 color light prefers dark perfers scheme

perfers-color-scheme

简介

prefers-color-scheme 媒体查询属性用于检测用户操作系统是否使用深色模式。

属性值

  • dark 表示用户操作系统使用深色模式
  • light 表示用户操作系统使用浅色模式
  • no-preference 表示用户操作系统没有偏好,或者操作系统不支持该属性

示例

@media (prefers-color-scheme: dark) {
  body {
    background: black;
    color: white;
  }
}

@media (prefers-color-scheme: light) {
  body {
    background: white;
    color: black;
  }
}
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统主题模式</title>
  </head>
  <body>
  </body>
</html>

JS 获取

const scheme = window.matchMedia('(prefers-color-scheme: dark)').matches
scheme ? console.log("用户系统使用深色模式") : console.log("用户系统使用浅色模式")

  • Mac 电脑可以在通用里设置模式切换查看效果 windows 自行百度如何切换主题模式进行尝试

color-scheme

  • light 表示可以使用操作系统亮色配色方案渲染元素。
  • dark 表示可以使用操作系统深色配色方案渲染元素。
  • no-preference 表示元素未指定任何配色方案,因此应使用浏览器的默认配色方案呈现。
  • only 禁止用户代理覆盖元素的颜色方案。

语法

color-scheme: normal;
color-scheme: light;
color-scheme: dark;
color-scheme: light dark;
color-scheme: only light;

/* 全局值 */
color-scheme: inherit;
color-scheme: initial;
color-scheme: revert;
color-scheme: revert-layer;
color-scheme: unset;
  • 备注:测试直接制定 表单元素有效

要将整个页面配置为使用用户的配色方案首选项,请在 :root 元素上指定 color-scheme。

:root {
  color-scheme: light dark;
}

浏览器兼容性

Alternative Text

参考文档

标签:操作系统,color,light,prefers,dark,perfers,scheme
From: https://www.cnblogs.com/yidejiyi/p/18232788

相关文章

  • css12 CSS HEX Colors
    https://www.w3schools.com/css/css_colors_hex.aspAhexadecimalcolorisspecifiedwith:#RRGGBB,wheretheRR(red),GG(green)andBB(blue)hexadecimalintegersspecifythecomponentsofthecolor.HEXValueInCSS,acolorcanbespecifiedusingahex......
  • css11 CSS RGB Colors
    css11CSSRGBColorshttps://www.w3schools.com/css/css_colors_rgb.aspAnRGBcolorvaluerepresentsRED,GREEN,andBLUElightsources.RGBValueInCSS,acolorcanbespecifiedasanRGBvalue,usingthisformula:rgb(red,green,blue)Eachparameter(......
  • css10 CSS Colors
    https://www.w3schools.com/css/css_colors.asp Colorsarespecifiedusingpredefinedcolornames,orRGB,HEX,HSL,RGBA,HSLAvalues.CSSColorNamesInCSS,acolorcanbespecifiedbyusingapredefinedcolorname: <!DOCTYPEhtml><html&g......
  • ColoredBox child 是 Scaffold 颜色失效
    在Flutter中,Scaffold小部件有自己的一组属性来管理其外观,包括背景颜色。当你将Scaffold作为ColoredBox的子小部件时,Scaffold的backgroundColor属性将覆盖ColoredBox的颜色。要解决这个问题,你可以直接设置Scaffold的backgroundColor属性,而不是使用ColoredBox。如......
  • linux-c-log-rotation-scheme
    linux-c-log-rotation-scheme#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>voidlogworker(){ino_tinode=0;FILE*logfile;logfile=fopen(logfilename,"a+");while(running){......
  • [SDCPC2023] Colorful Segments 线段树转移DP
    Codeforces链接​  洛谷题目链接#[SDCPC2023]ColorfulSegments##题面翻译**【题目描述】**考虑数轴上的$n$条线段,其中第$i$条线段的左端点为$l_i$,右端点为$r_i$。每一条线段都被涂上了颜色,其中第$i$条线段的颜色为$c_i$($0\lec_i\le1$)。颜色共有两种,$c_i......
  • 【源码翻译之交互式对象包 AIS-AIS_ColoredShape.hxx文件 多颜色交互式对象
    类AIS_ColoredShape形状的呈现具有可自定义的子形状属性。此类可以将topods的子拓扑分别设置不同的颜色然后作为一个整体显示成员类型定义文档◆DataMapOfDrawerCompdtypedefNCollection_IndexedDataMap<Handle<AIS_ColoredDrawer>,TopoDS_Compound,TColStd_MapT......
  • Topcoder SRM616-Div1-Lv2 ColorfulCoins
    涉及知识点:奇妙Ad-hoc前言一道很不常规的题目,思维难度大代码简单,而且这种思路很难在赛时想到,故记录一下。题意某国的货币系统硬币有\(n\(\leq60)\)种面额\(val_i\(\leq10^{18})\),其中最小的面额为\(1\),并且所有的面额之间都保证两两有倍数关系,每种面额的硬币有独一无......
  • HTML 21 - Colors
     HTMLColorsareawayofspecifyingtheappearanceofwebelements.Colorsareveryimportantaspectsofwebdesign,astheynotonlyenhancethevisualappealbutalsoinfluenceuserbehavior.Theyarealsousedtoevokeemotionsandhighlightimportan......
  • WPF Color ColorConverter.ConvertFromString convert hex to readable color
    stringcolorStr="#FF00008B";ColorbrushColor=(Color)ColorConverter.ConvertFromString(colorStr);  usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;us......