• 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

[Godot RPG] #4 : Tilemap
Tips

[Godot RPG] #4 : Tilemap

November 25, 2025
Tips to help find hidden cameras in the room
Tips

Tips to help find hidden cameras in the room

November 24, 2025
Experience in buying Laptops for Students and Office Workers
Tips

Experience in buying Laptops for Students and Office Workers

November 23, 2025
The fastest way to free up Gmail space
Tips

The fastest way to free up Gmail space

November 22, 2025
How to fix SSD disappearing due to new Windows 11 update
Tips

How to fix SSD disappearing due to new Windows 11 update

November 22, 2025
How to use Nano Banana Pro to create posters and infographics in Vietnamese
Tips

How to use Nano Banana Pro to create posters and infographics in Vietnamese

November 21, 2025
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

[Godot RPG] #4 : Tilemap

[Godot RPG] #4 : Tilemap

November 25, 2025
Instructions for creating super romantic QR love effects

Instructions for creating super romantic QR love effects

November 25, 2025
Tips to help find hidden cameras in the room

Tips to help find hidden cameras in the room

November 24, 2025
Experience in buying Laptops for Students and Office Workers

Experience in buying Laptops for Students and Office Workers

November 23, 2025
[Godot RPG] #4 : Tilemap

[Godot RPG] #4 : Tilemap

November 25, 2025
Instructions for creating super romantic QR love effects

Instructions for creating super romantic QR love effects

November 25, 2025
Tips to help find hidden cameras in the room

Tips to help find hidden cameras in the room

November 24, 2025
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

[Godot RPG] #4 : Tilemap

[Godot RPG] #4 : Tilemap

November 25, 2025
Instructions for creating super romantic QR love effects

Instructions for creating super romantic QR love effects

November 25, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

trang chủ f168 nhà cái 78win nhà cái 78win https://www.qq8827.com/

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

trang chủ f168 nhà cái 78win nhà cái 78win https://www.qq8827.com/

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