Dark Mode with Media Query!ππ―
Yes you heard it right, We can develop Dark mode for our websites with CSS.
By using prefers-scheme-color media query .π
Prefers-scheme-color
The prefers-scheme-color CSS Media feature is used to detect, if the user has request the system to use a light or dark color theme.β
//Syntax
@media(prefers-scheme-color : dark)
{}
Customize Properties
Customize Properties or CSS Variables are entities defined by CSS Authors that contain specific values to be re-used throughout the document.
//Syntax
:root {
--background: grey;
}
Combining Media Query with CSS Variables (1st Method)
Default CSS Variables are defined at the root, prefers-scheme-color will check for dark mode and use the new CSS Variable.
:root {
--backgroundChange : grey;
}@media (prefers-scheme-color) {
:root {
--backgroundChange : black;
}
}
Why to use Dark Mode?!π
Inclusive Design : π¨
For those who face difficulty to high contrast white backgrounds & dark texts.
Battery Life :π»
Dark Mode Apps/Websites can prolong the battery life of your smartphones as well as your desktops.
Content Focused :π
Dark Mode also puts the focus your content allowing it to stand out from the surrounding UI.
2nd Method :
You can directly use prefers-scheme-color in your CSS file too, In demo Iβll be using directly this media query in my styles.css
Thanks for reading!π£
Yours in coding,
Khadija Batool Gardezi.