47
loading...
This website collects cookies to deliver better user experience
Dark mode is better for your eyes in low-light environments and is better for your device battery.
@media (prefers-color-scheme: dark)
media query, We can detect if the user has requested a light or dark color theme by User Agent(based on OS settings).@media (prefers-color-scheme: dark) {
:root {
--main-bg-color: #040404;
--card-bg-color: #3a3a38;
--icon-bg-color: #f7d1a2;
--main-light-onColor: #aeb4bd;
--main-dark-onColor: #777676;
}
}
/* we can also style for light mode*/
@media (prefers-color-scheme: dark) {
}
:root
Selects the root element of the document: in the case of HTML and defined the custom property
inside :root
means which can be declared and used globally in document. Use Custom property
is a best practice which save our loads of time like Life saver.If you not aware of Custom property
, Not a big issue. I will explain, else you just skip this fold and pass on to next fold.
A common best practice is to define custom properties on the :root
pseudo-class, so that it can be applied globally across your HTML document.
:root {
--main-bg-color: brown;
}
custom property
is pretty easy right. Likewise, Use this custom property is painless process😉. Just you use the custom property value by specifying your custom property
name inside the var()
function, in place of a regular property value.header{
background-color: var(--main-bg-color);
}
color-scheme
- Must includecolor-scheme
changes the default text and background colors of the page to match the current system appearance, standard form controls, scrollbars and other named system colors also change their look automatically.:root {
color-scheme: dark light;
}
/* possible property values */
color-scheme: normal;
color-scheme: light;
color-scheme: dark;
color-scheme: light dark;
color-scheme
specifying the values light and dark on the root element. let's the rendering engine known both modes are supported by the document.