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
The MySQL database does not have an incremental b...
Introduction Closure is a very powerful feature i...
When using Dreamweaver or FrontPage to create HTM...
Table of contents 1. Basics 1.ref 2. toRef 3. toR...
Check whether your cuda is installed Type in the ...
Table of contents What is axios? Axios request ty...
Table of contents js deep copy Data storage metho...
The property names often heard in web design: con...
1. Inner Join Query Overview Inner join is a very...
This article introduces how to install MySQL 8.0 ...
Table of contents 1. Use slots to make components...
The project requirements are: select date and tim...
Problem Description After installing workstations...
The installation of MySQL 5.7 on Ubuntu 1804 is i...
Before talking about data responsiveness, we need...