In the fifth issue of Web Skills, a technical solution for implementing dark mode and highlight mode with CSS is specifically mentioned. That is, use the new media query condition What are dark mode and light mode Before talking about technical solutions, let’s first briefly understand what dark mode and highlight mode are? These two concepts come from the macOS system, which provides users with two theme skins, namely light and dark skins. Since this concept came into being, many websites have provided users with two sets of skin colors, making it easy for users to switch according to their habits or preferences. Whether it is dark mode or highlight mode, it is a switch between black and white. This theme style is very friendly to users with color blindness. Similar functions can be found in other systems or software, the difference lies in the modes provided. In some software, users may be provided with some skin customization functions. Of course, there are similar functions on websites, but in the past we may prefer to call this function website skinning . In this way, we can talk about the switching between these two modes as changing the skin, which may be more suitable for our business scenarios. Next, let’s talk about the technical aspects, that is, how to use CSS to complete the theme switching of web pages or applications! Simplest mode Assuming that your theme is in highlight mode by default, we can use the simplest and most brutal way to switch highlight mode to dark mode. Suppose there is an entry on the web page, and when the user clicks this document.getElementById('buttonID').addEventListener('click', function(){ document.documentElement.classList.add('dark-theme') }) Add dark styles to .dark-theme { background-color: #000; color: white; } .dark-theme *:not(a) { background-color: #000 !important; color: #fff !important; border-color: #999 !important; } Although this method is simple and crude, some details need additional processing, especially when the code also uses Prepare two sets of styles In my personal impression, the earliest way to achieve similar effects was generally to use JavaScript to change the As shown in the figure above, two CSS files are provided, one is Assume that the default theme style of the web page is <link type="text/css" rel="stylesheet" media="all" href="../theme1.css" id="theme_css" /> Provide a simple script function in the code: document.getElementById('buttonID').addEventListener('click', function(){ document.getElementById('theme_css').href = '../theme2.css'; }) Back to our theme, if you need to switch between dark mode and light mode, you can follow a similar principle and provide Maintaining multiple sets of styles is a pain, especially when you want to provide more skins for your product. At this time, you can use a processor like Sass to maintain your theme style, declare variables, and then maintain the corresponding variable values. For example, the construction of the Bootstrap theme uses Sass variables: The knowledge of managing multiple skins with Sass is beyond the scope of this article. If you are interested in this knowledge, you can read the following articles: Organizing Multiple Theme Styles with Sass Summarize The above is what I introduced to you about using CSS to switch between dark mode and highlight mode. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time! |
<<: JavaScript implements double-ended queue
>>: Solve the problem of Mac Docker x509 certificate
Table of contents 1. Demo Project 1.1 Interface P...
Table of contents Preface 1. Array traversal meth...
Use the Linux chmod command to control who can ac...
1.17.9 More delicious, really Nginx download addr...
This status code provides information about the s...
The default MySQL version under the Alibaba Cloud...
Table of contents Preface 1. Style penetration 1....
1. To optimize the query, try to avoid full table...
Sometimes the theme of a project cannot satisfy e...
Pure js implements a single-click editable table ...
background The amount of new data in the business...
Hello everyone, I wonder if you have the same con...
This article shares the specific code of Vue+Webs...
Table of contents 1. What is Set 2. Set Construct...
Create your first web page in one minute: Let'...