Recently, WeChat was forced by Apple to develop a dark mode . More and more websites and applications have begun to support dark mode. Many people also like to choose dark mode for websites. Maybe they prefer this appearance, or they want to protect their eyes from fatigue. This post will show you how to implement an automatic CSS dark mode that changes based on your visitor’s theme. I tried implementing dark mode using CSS Dark Mode I defined variables to set the colors of your theme, and I recommend you do the same as it will make the process a lot easier. My default color variables are as follows: :root { --accent: #226997; --main: #333; --light: #666; --lighter: #f3f3f3; --border: #e6e6e6; --bg:#ffffff; } If you want to use these variables in your stylesheet, you can do it like this: p { color: var(--main); } This way, if you want to change the colors of your theme, you only need to modify the defined variable and everything that uses that variable will update. Now we need to define a new set of variables that will be used when invoking CSS dark mode. /* Define the color of dark mode */ :root { --accent: #3493d1; --main: #f3f3f3; --light: #ececec; --lighter: #666; --border: #e6e6e6; --bg: #333333; } Added Dark support Now that we have two sets of variables defined, the only thing left to do is add Take the Dark color variable and add the following /* Define the color of dark mode */ @media (prefers-color-scheme: dark) { :root { --accent: #3493d1; --main: #f3f3f3; --light: #ececec; --lighter: #666; --border: #e6e6e6; --bg: #333333; } } That's it! If someone is using a dark OS theme and visits your website, your website will now automatically switch to dark mode. I'm sure you'll want to test whether this change works. To do this, you can simply enable a dark theme on your operating system, such as the iOS dark theme. Alternatively, if you don’t want to waste time with theming your OS, you can force this test in Firefox. Here’s how: Open Firefox and type You will be asked to take the risk, accept it. In the search bar, search for The page should now be black. Go back to your website and the theme should have automatically updated to dark mode. If you want to test whether it switches back, change the value to When you are finished testing, click the trash can to delete the option. This concludes this article about sample code for implementing dark mode with CSS variables. For more relevant CSS dark mode content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: 2 reasons why html-css tag style setting does not work
>>: Why Seconds_Behind_Master is still 0 when MySQL synchronization delay occurs
Pure js implements a single-click editable table ...
Table of contents Skeleton screen use Vue archite...
Purpose: 1. In order to map the server's exte...
This article shares the specific code of js to ac...
Preface The reduce() method receives a function a...
A registration page template implemented with HTM...
Table of contents Preface 1. Routing lazy loading...
In applications with paging queries, queries that...
Implementation Preparation # Need to back up the ...
mysql installer community 8.0.16.0 installation g...
As a useful terminal emulator, Xshell is often us...
NAT In this way, the virtual machine's networ...
Table of contents 2. Detailed explanation 2.1. Ad...
There are too much knowledge to learn recently, a...
This article shares the specific code of jQuery t...