Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save willmendesneto/344f4226cfd30e24027b7d8f6b96020d to your computer and use it in GitHub Desktop.
Save willmendesneto/344f4226cfd30e24027b7d8f6b96020d to your computer and use it in GitHub Desktop.
dark-mode via css media query and classes via javascript
/** Common CSS */
html {
background: #FFFFFF;
}
html.dark-mode {
filter: invert(100%);
}
html.dark-mode img {
filter: invert(100%);
}
// Detect if the user has requested the system
// use a light or dark color theme.
// More details about `prefers-color-scheme` in https://web.dev/prefers-color-scheme/
@media (prefers-color-scheme: dark) {
// Important
// you should add dark-mode if your html element
// does not have `.light-mode` class in place
// so you page can still respect the dark theme configuration
// from your user system
html:not(.light-mode) {
filter: invert(100%);
}
html:not(.light-mode) img {
filter: invert(100%);
}
}
/** Common CSS */
/** Switch styles */
.switch-toggle {
/** The display value will be changes via Javascript.
So it the browser supports media query manipulation,
it shows this option */
display: none;
flex-direction: row;
margin-bottom: 45px;
margin-top: 20px;
min-height: 50px;
}
.switch-toggle input[type=checkbox] {
height: 0;
width: 0;
visibility: hidden;
}
.switch-toggle label {
cursor: pointer;
text-indent: -9999px;
width: 100px;
height: 50px;
background: #bada55;
display: block;
border-radius: 50px;
position: relative;
}
.switch-toggle label:after {
content: "";
position: absolute;
top: 5px;
left: 5px;
width: 40px;
height: 40px;
background: #fff;
border-radius: 40px;
transition: 0.3s;
}
.switch-toggle input:checked + .active {
background: #bada55;
}
.switch-toggle input:checked + .active:after {
left: calc(100% - 5px);
transform: translateX(-100%);
}
.switch-toggle label:after {
width: 65px;
}
/** Switch styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment