Step by step instructions to make 2 versions of the website to make it suitable for day and night use
Update: I now use media query level 5:prefers-color-scheme
ThisMedia Queries Level 5specificationContains a newprefers-color-scheme
Media function.
All major browsers currently support this feature. Chrome/Edge since version 76, Firefox and Safari since version 67 (since version 12.1). Even iOS Safari,
We can use it to determine whether the user is browsing the page in dark or light mode:
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
@media (prefers-color-scheme: light) {
body {
background-color: white;
color: black;
}
}
Now, I use it by default to display the light version of the site, and use the dark version if the system is in dark mode.
If the system does not have a built-in dark mode (old Windows/macOS, Linux), then my suggestion is to use an extension, such asNight eyeOr similar.
How did I implement dark mode beforeprefers-color-scheme
Available
I recently redesigned my website. Here are 2 pictures for reference:
I designed this website almost 1 year ago and made many changes in the process, just like we do for any website.
Eventually I grew up with this design: the title was too big, lost too much space instead of displaying the content directly, etc.
I sat down last night and started to redesign the website, and completed the redesign this morning:
much better! The most important thing is that the content is more prominent.
I used a monospaced font (Inconsolata), because as a programming blog, it is a good blog, although the use of fonts leads to reduced readability and increased page size, because Imiss youThe font is on my website. I like it better, and since my website is an important part of my daily activities, I want it to be as I want it to be.
I just missed one thing:Dark mode. During the redesign, I thought of the dark mode option.
How did i do it? First, I added Moon Emoji Moon in the sidebar to make people change the mode from light to dark.
Then, I added a JavaScript snippet that runs when clicked. I just added it toonclick
Inline event handlers in HTML without fancy:
<p>
<a href="#" onclick="localStorage.setItem('mode', (localStorage.getItem('mode') || 'dark') === 'dark' ? 'light' : 'dark'); localStorage.getItem('mode') === 'dark' ? document.querySelector('body').classList.add('dark') : document.querySelector('body').classList.remove('dark')" title="Dark/light
</p>
This is the JavaScript running in onclick:
localStorage.setItem('mode', (localStorage.getItem('mode') || 'dark') === 'dark' ? 'light' : 'dark'); localStorage.getItem('mode') === 'dark' ? document.querySelector('body').classList.add('dark') : document.querySelector('body').classList.remove('dark')
This is a bit puzzling, but basically I will checkmode
PropertyLocal storageIs "dark" (if it has not been set, the default is dark, use||
Operator), and then set the opposite value in the local storage device.
Then i assigndark
Go to classbody
HTML elements, so we can use CSS to style the page in dark mode.
After the DOM is loaded, another script runs immediately and checks whether the mode is dark. If so, it will adddark
Go to classbody
HTML elements:
document.addEventListener('DOMContentLoaded', (event) => {
((localStorage.getItem('mode') || 'dark') === 'dark') ? document.querySelector('body').classList.add('dark') : document.querySelector('body').classList.remove('dark')
})
Now, if people change the mode, their choice will be remembered the next time the page loads.
Then I added a lot of CSS instructions to the CSS, and all the instructions start withbody.dark
. Like these:
body.dark {
background-color: rgb(30,34,39);
color: #fff;
}
body.dark code[class*=language-],
body.dark table tbody>tr:nth-child(odd)>td,
body.dark table tbody>tr:nth-child(odd)>th {
background: #282c34
}
Things should be normal now! This is my website in dark mode:
I addeddark
Go to classbody
By default, the element makes dark mode the default setting
<body class="dark">
...
</body>
why? First of all, I like it better. Then, I conducted a poll on Twitter and people liked it better.
But this is also for technical reasons, which is actually a very simple reason. I will not store user-selected content on the server side, so I cannot know the mode until local storage is available.
If the site is generated on the server side, then I can do this, but it is a static site, so I always provide the same page to everyone who requests it. Even if I have a cookie, there is nowhere to deal with it (on the other hand, this means my page loads faster).
Therefore, when someone navigates to another page on my website, or loads that page for the first time in a second visit, I don't want to display a bright page in the OK mode. Maybe the visitor was coding in the dark room in the middle of the night.
I would rather do this in light mode: display a dark page for a few milliseconds, then turn it white again.
More experimental tutorials:
- The stack I use to run this blog
- 8 good reasons to become a software developer
- SEO for blogging developers
- Recalling "4-Hour Work Week"
- Build a lifestyle business
- Build your own platform
- As an independent manufacturer, which product should you manufacture?
- Create your own job security
- Developers, learning marketing
- Product business freedom
- Generate value
- For your business
- The idea is nothing
- Niche Market
- Remote work for software developers
- Product/market fit
- The best podcasts for front-end developers
- Why create an email list?
- Break the link between time and money
- The scarcity principle applies to software products
- Social proof principle
- How do I add dark mode to my website
- My notes on Deep Work
- Advantages of using a boring stack
- How to estimate programming time
- Become an independent developer
- How to learn how to learn
- Why are interview questions for programming jobs so difficult?
- Do I need a degree to become a programmer?
- Everyone can learn programming
- How to increase productivity
- How to get the actual pageviews of static websites
- Have you filled the developer’s bucket today?
- How do i record my video
- All the software projects I have done in the past
- Tutorial Purgatory from the Perspective of Tutorial Makers
- Every developer should have a blog. This is why and how to stick to it
- Have the business mindset of a developer
- How to write unmaintainable code
- What is imposter syndrome
- How to work from home without going crazy
- How can I stop worrying and learn to love the JavaScript ecosystem
- How do I make a web prototype
- You should be the worst developer on the team
- How to start a blog with Hugo
- Write things you don't know
- How to use uBlock Origin to stop interference
- Coding is an art
- I wrote a blog post every day for two consecutive years. Here are 5 things I learned from SEO
- Put out the fire
- About becoming a generalist
- The developer's dilemma
- My plan for being hired as a Go developer. In 2017
- Use Mac and iOS devices to improve work efficiency
- How to move from the tutorial to your own project
- This is my little digital garden
- How to start freelancing as a developer
- Share the journey of building a software product business
- Subfolders and subdomains
- How can I use text extensions to save time
- Software is superpower
- I like books
- How do I decide to create a new project management application