• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

Some simple CSS tricks to enable Dark Mode for Websites

AnonyViet by AnonyViet
January 26, 2023
in Tips
0

You may already be familiar with media queries. They are being widely used to make responsive websites. The width and height properties contain the dimensions of the screen. You will then use CSS to render different layouts at different screen sizes.

Join the channel Telegram of the AnonyViet 👉 Link 👈

Some simple CSS tricks to enable Dark Mode for Websites

Media query prefers-color-scheme works in a similar way. Users can configure their operating system to use light or dark mode. Prefers-color-scheme contains that value. The value is light or dark, although the W3C spec says it may support other values ​​in the future like sepia. We will specify different values ​​of CSS variables for both modes and let the user’s operating system decide.

Some simple CSS tricks for dark mode

Using media queries prefers-color-scheme

Two values ​​of media query prefers-color-scheme to be:

/* 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 code, –body-bg and –body-color are CSS variables. As you can see, they contain different values ​​for both modes. In light mode, I set a white background with black text. In dark mode, I set the background to black with white text.

Since the W3C may introduce values ​​in the future, it makes sense to convert this CSS to a boolean.

/* 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, I am defining light mode by default and converting it to dark mode if the media query is dark (i.e. the user uses dark mode on the operating system). This way, any future values ​​added to the media query will set light mode by default.

Using CSS variables

Now that we have different values ​​for different themes, we will use them to style the page.

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

Syntax var() is how CSS uses variables. In the above code, I am saying to set the background to the value of –body-bg and set the color to the value of –body-color. Note that the values ​​of these variables come from the media query. Means background and background colors based on operating system settings.

This is the true power of media queries: Deliver a consistent user experience from the operating system to the website.

If you visit findmymastodon.com and switch the operating system theme, you will see the website switch from one theme to another.

Conclusion

Note that the use of prefers-color-scheme It’s no different than using a regular programming language. We define variables whose values ​​change based on some logic. And those variables are used for further operations.

The ability to allow your site to adapt to a user-selected theme is a great feature. And it further blurs the line between desktop and web for the benefit of users. Latest browser versions supported prefers-color-schemeso you can start trying it out today.

The article achieved: 5/5 – (100 votes)

Tags: CSSDarkEnableModesimpletricksWebsites
Previous Post

How to automatically change Dark and Light wallpaper on Android

Next Post

Lesson 12: Formulas and Functions – Basic Excel Formulas and Functions

AnonyViet

AnonyViet

Related Posts

How to change the default font on Windows 10
Tips

How to change the default font on Windows 10

April 13, 2026
5 tips for using a Browser to replace an App (helps save RAM, time and money)
Tips

5 tips for using a Browser to replace an App (helps save RAM, time and money)

April 13, 2026
How to make funny MeMe photos without Photoshop within 10 seconds
Tips

How to make funny MeMe photos without Photoshop within 10 seconds

April 11, 2026
How to quickly design your own Logo without Photoshop
Tips

How to quickly design your own Logo without Photoshop

April 10, 2026
How to convert Website into App on Windows
Tips

How to convert Website into App on Windows

April 9, 2026
Instructions for getting 3 months of Adobe Express Pro for free
Tips

Instructions for getting 3 months of Adobe Express Pro for free

April 9, 2026
Next Post
Lesson 12: Formulas and Functions – Basic Excel Formulas and Functions

Lesson 12: Formulas and Functions - Basic Excel Formulas and Functions

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Japanese Watch Week: Choose the color that suits your destiny, start the new year well at Mobile World

Japanese Watch Week: Choose the color that suits your destiny, start the new year well at Mobile World

April 20, 2026
4K Video Downloader Plus – Tool to download YouTube videos on macOS

4K Video Downloader Plus – Tool to download YouTube videos on macOS

April 20, 2026
How to add Watermark to Google Docs to protect copyright

How to add Watermark to Google Docs to protect copyright

April 19, 2026
How to intercept traffic using Burp Suite to analyze HTTP/HTTPS

How to intercept traffic using Burp Suite to analyze HTTP/HTTPS

April 18, 2026
Japanese Watch Week: Choose the color that suits your destiny, start the new year well at Mobile World

Japanese Watch Week: Choose the color that suits your destiny, start the new year well at Mobile World

April 20, 2026
4K Video Downloader Plus – Tool to download YouTube videos on macOS

4K Video Downloader Plus – Tool to download YouTube videos on macOS

April 20, 2026
How to add Watermark to Google Docs to protect copyright

How to add Watermark to Google Docs to protect copyright

April 19, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Japanese Watch Week: Choose the color that suits your destiny, start the new year well at Mobile World

Japanese Watch Week: Choose the color that suits your destiny, start the new year well at Mobile World

April 20, 2026
4K Video Downloader Plus – Tool to download YouTube videos on macOS

4K Video Downloader Plus – Tool to download YouTube videos on macOS

April 20, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply