DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • The Observability Gap: Why Your Monitoring Strategy Isn't Ready for What's Coming Next
  • WebSockets, gRPC, and GraphQL in the Core
  • Scaling Teams, Scaling Systems: Unlocking Developer Productivity With Platform Engineering
  • Engineering Complexity: Implied vs. Induced Complexity

Creating Light and Dark Themes for Websites The Right Way Using prefers-color-scheme

Blur the line between desktop and web by letting the OS style your website or web app using prefers-color-scheme for light and dark themes.

By 
Ayush Sharma user avatar
Ayush Sharma
·
Nov. 04, 21 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

You’re likely already familiar with media queries. They’re in widespread use for making websites responsive. The width and height properties contain the viewport’s dimensions. We then use CSS to render different layouts at different dimensions.

The prefers-color-scheme media query works the same way. The user can configure their operating system to use a light or dark theme. prefers-color-scheme contains that value. The value is either light or dark, though the W3C spec states that it might support future values like sepia. We specify different values of CSS variables for both modes and let the user’s OS decide.

The prefers-color-scheme Media Queries

The two variations of the prefers-color-scheme media query are:

JavaScript
 
/* Light mode */
@media (prefers-color-scheme: light) {
   :root {
       --body-bg: #FFFFFF;
       --body-color: #000000;
   }
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
   :root {
       --body-bg: #000000;
       --body-color: #FFFFFF;
   }


In the above CSS, --body-bg and --body-color are CSS variables. As you can see, they contain different values for both modes. In the light theme, we’re setting a white background with black text. In the dark theme, we’re setting a black background with white text.

Since the spec says that W3C might introduce future values, it makes sense to convert our CSS into a boolean.

JavaScript
 
/* Light mode */
:root {
   --body-bg: #FFFFFF;
   --body-color: #000000;
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
   :root {
       --body-bg: #000000;
       --body-color: #FFFFFF;
   }
}


In the above code, we’re defining a light theme by default and converting to the dark theme if the media query is dark. This way, any future values added to the media query will set the light theme by default.

Using the CSS Variables

Now that we have different values for different themes, we need to actually use them to style our page.

JavaScript
 
body {
   background: var(--body-bg);
   color: var(--body-color);
}


The var() syntax is how CSS uses variables. In the above code, we’re saying that background should be set to the value of --body-bg. And color should be set to the value of --body-color. Note that the values of these variables are coming from our media query, meaning that our background and foreground colour changes based on the OS’s setting! This is the real power of the media query: providing a consistent user experience from OS to the web page.

If you go to findmymastodon.com and switch your OS’s theme, you’ll see the transition from one theme to another. The video below shows the smooth transition from light mode to dark mode on Mac OS X.

Your browser does not support this video.

The CSS WG website also uses the same media queries. Change your OS theme and the website will switch themes to adjust.

Conclusion

Notice that using prefers-color-scheme is no different from using a regular programming language. We define variables whose values change based on some logic. Those variables are then used for further operations.

The ability to let your website adjust to the user’s theme of choice is a great accessibility feature. It also further blurs the line between desktop and web for the benefit of the user. Latest browser versions support prefers-color-scheme, so you can begin experimenting today.

Light (web browser)

Published at DZone with permission of Ayush Sharma. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook