Git Product home page Git Product logo

css-prefers-color-scheme's Introduction

Prefers Color Scheme

NPM Version Build Status Support Chat

Prefers Color Scheme lets you use light and dark color schemes in all browsers, following the Media Queries specification.

Usage

From the command line, transform CSS files that use prefers-color-scheme media queries:

npx css-prefers-color-scheme SOURCE.css TRANSFORMED.css

Next, use that transformed CSS with this script:

<link rel="stylesheet" href="TRANSFORMED.css">
<script src="https://unpkg.com/css-prefers-color-scheme/browser.min"></script>
<script>
colorScheme = initPrefersColorScheme('dark') // apply "dark" queries (you can change it afterward, too)
</script>

Dependencies got you down? Don’t worry, this script is only 537 bytes.

Usage

  • First, transform prefers-color-scheme queries using this PostCSS plugin.
  • Next, apply light and dark color schemes everywhere using this browser script.

How does it work?

Prefers Color Scheme uses a PostCSS plugin to transform prefers-color-scheme queries into color-index queries. This changes prefers-color-scheme: dark into (color-index: 48), prefers-color-scheme: light into (color-index: 70), and prefers-color-scheme: no-preference into (color-index: 22).

The frontend receives these color-index queries, which are understood in all major browsers going back to Internet Explorer 9. However, since browsers only apply color-index queries of 0, our color scheme values are ignored.

Prefers Color Scheme uses a browser script to change (color-index: 48) queries into not all and (color-index: 48) in order to activate “dark mode” specific CSS, and it changes (color-index: 70) queries into not all and (color-index: 48) to activate “light mode” specific CSS.

@media (color-index: 70) { /* prefers-color-scheme: light */
  body {
    background-color: white;
    color: black;
  }
}

Since these media queries are accessible to document.styleSheet, no CSS parsing is required.

Why does the fallback work this way?

The value of 48 is chosen for dark mode because it is the keycode for 0, the hexidecimal value of black. Likewise, 70 is chosen for light mode because it is the keycode for f, the hexidecimal value of white.

css-prefers-color-scheme's People

Contributors

jonathantneal avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.