• 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

Get free Cambridge courses to prepare for IELTS, Starters, Movers
Tips

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to record reaction videos with Android phones, no app needed
Tips

How to record reaction videos with Android phones, no app needed

June 1, 2026
Instructions on how to get Google AI Pro 1 year for free for new accounts
Tips

Instructions on how to get Google AI Pro 1 year for free for new accounts

June 1, 2026
Top free AI tools to help write, test and optimize content
Tips

Top free AI tools to help write, test and optimize content

June 1, 2026
Instructions for receiving Lovable Pro 12 months for free
Tips

Instructions for receiving Lovable Pro 12 months for free

May 29, 2026
Instructions for receiving 1 month of ElevenLabs Creator for free worth
Tips

Instructions for receiving 1 month of ElevenLabs Creator for free worth $22

May 25, 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

How to convert HEIC photos to JPG on iPhone without using an app

How to convert HEIC photos to JPG on iPhone without using an app

June 5, 2026
Get free Cambridge courses to prepare for IELTS, Starters, Movers

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to check network speed on Apple TV when the image is jerky or lagging

How to check network speed on Apple TV when the image is jerky or lagging

June 3, 2026
Instructions on how to prevent acquaintances from seeing your Tiktok

Instructions on how to prevent acquaintances from seeing your Tiktok

June 2, 2026
How to convert HEIC photos to JPG on iPhone without using an app

How to convert HEIC photos to JPG on iPhone without using an app

June 5, 2026
Get free Cambridge courses to prepare for IELTS, Starters, Movers

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to check network speed on Apple TV when the image is jerky or lagging

How to check network speed on Apple TV when the image is jerky or lagging

June 3, 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 convert HEIC photos to JPG on iPhone without using an app

How to convert HEIC photos to JPG on iPhone without using an app

June 5, 2026
Get free Cambridge courses to prepare for IELTS, Starters, Movers

Get free Cambridge courses to prepare for IELTS, Starters, Movers

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

6789 kv999

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

6789 kv999

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