• 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 use Messenger without a Facebook account
Tips

How to use Messenger without a Facebook account

October 22, 2026
The website helps you draw beautifully like TikToker Viet My
Tips

The website helps you draw beautifully like TikToker Viet My

October 21, 2026
Instructions on how to hard reset iPhone 18 Pro / iPhone 18 Pro Max
Tips

Instructions on how to hard reset iPhone 18 Pro / iPhone 18 Pro Max

October 21, 2026
Anyone can do how to increase Internet speed using CMD
Tips

Anyone can do how to increase Internet speed using CMD

October 20, 2026
How to report a fraudulent website on Weebly
Tips

How to report a fraudulent website on Weebly

October 19, 2026
How to view Facebook stories without others knowing
Tips

How to view Facebook stories without others knowing

October 18, 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

Recent News

How to use Messenger without a Facebook account

How to use Messenger without a Facebook account

October 22, 2026
Instructions on how to exit iOS 27 Beta and update to the official version

Instructions on how to exit iOS 27 Beta and update to the official version

October 22, 2026
Instructions for updating PHP to avoid PHP2Shell

Instructions for updating PHP to avoid PHP2Shell

October 22, 2026
The website helps you draw beautifully like TikToker Viet My

The website helps you draw beautifully like TikToker Viet My

October 21, 2026
How to use Messenger without a Facebook account

How to use Messenger without a Facebook account

October 22, 2026
Instructions on how to exit iOS 27 Beta and update to the official version

Instructions on how to exit iOS 27 Beta and update to the official version

October 22, 2026
Instructions for updating PHP to avoid PHP2Shell

Instructions for updating PHP to avoid PHP2Shell

October 22, 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

How to use Messenger without a Facebook account

How to use Messenger without a Facebook account

October 22, 2026
Instructions on how to exit iOS 27 Beta and update to the official version

Instructions on how to exit iOS 27 Beta and update to the official version

October 22, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

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