Hello Coders! Welcome to CSS Animation Blog. In this, we're going to see how to create a theme toggle which allows you to change light and dark mode by a click of a toggle in Pure CSS. Dark mode is now a trend in Website Design and Developers are also adding a dark mode feature in all the webapps. This is a sample version of the same. You can see the body having title and paragraph. On top of it, there is a toggle which allows us to change the theme of the window. Hope you'll like it.
Here's a preview-
That being said, let us get started.
Step - 1: Like always, create 2 files - index.html and style.css.
Step - 2: Copy the below HTML code and paste it into your code editor.
HTML
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>theme-toggle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./style.css">
</head>
<body>
<body>
<div class="container">
<h1>This is title</h1>
<div class="toggle-container">
<input type="checkbox" id="switch" name="theme" /><label for="switch" id="toggle">Toggle</label>
</div>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Architecto, sed. Minima distinctio consequatur aperiam quo ratione hic, neque quisquam nulla. Alias eum, quicksand quidem corporis accusamus fugit cumque fugiat modi?</p>
</div>
</body>
<script src="./script.js"></script>
</body>
</html>
Step - 3: Below is the CSS code for styling.
CSS
@import url("https://fonts.googleapis.com/css?family=Quicksand");
html {
height: 70%;
font-family: 'Quicksand', sans-serif;
display: -ms-grid;
display: grid;
align-items: center;
justify-content: center;
/* settings for light theme,
vanilla css variables */
--bg: #ffffff;
--bg-panel: #ffffff;
--color-heading: #0077FF;
--color-text: #6d6d6d;
--color-toggle-border: #dadada;
--color-toggle: #b1b1b1;
}
/* remove focus outline */
*:focus {
outline: none;
}
html .container {
box-shadow: 0px 45px 133px -25px rgba(0, 0, 0, 0.1);
}
/* settings for dark theme */
html[data-theme="dark"] {
--bg: #23272b;
--bg-panel: #16191c;
--color-heading: #3694FF;
--color-text: rgb(102, 102, 102);
--color-toggle-border: #353535;
--color-toggle: #525252;
}
html[data-theme="dark"] .container {
box-shadow: 0px 45px 133px -25px rgba(0, 0, 0, 0);
}
body {
background-color: var(--bg);
}
.container {
background-color: var(--bg-panel);
margin: 5em;
padding: 5em;
border-radius: 10px;
max-width: 800px;
display: -ms-grid;
display: grid;
-ms-grid-columns: 80% auto;
grid-template-columns: 80% auto;
-ms-grid-rows: auto auto;
grid-template-rows: auto auto;
grid-template-areas: "title switch" "content content";
}
.container h1 {
margin: 0;
color: var(--color-heading);
}
.container p {
color: var(--color-text);
-ms-grid-row: 2;
-ms-grid-column: 1;
-ms-grid-column-span: 2;
grid-area: content;
font-size: 1.1em;
line-height: 1.8em;
margin-top: 2em;
}
/* toggle switch */
input[type=checkbox] {
height: 0;
width: 0;
visibility: hidden;
}
label {
cursor: pointer;
border-style: solid;
border-color: var(--color-toggle-border);
text-indent: -9999px;
width: 52px;
height: 26px;
background: var(--bg-panel);
float: right;
border-radius: 100px;
position: relative;
transition: 0.5s;
}
label:after {
content: '';
position: absolute;
top: 4px;
left: 4px;
width: 18px;
height: 18px;
background: var(--color-toggle);
border-radius: 90px;
}
input:checked+label {
background: var(--bg-panel);
}
input:checked+label:after {
left: calc(100% - 4px);
transform: translateX(-100%);
}
/* transitions */
html.transition,
html.transition *,
html.transition *:before,
html.transition *:after {
transition: all 750ms !important;
transition-delay: 0 !important;
}
@media only screen and (max-width: 600px) {
.container {
margin: 2em;
padding: 2em;
}
}
Step - 3: Below is the JS code for applying the toggle functionality.
JS
const checkbox = document.querySelector('input[name=theme]');
setThemeOnLoad();
checkbox.addEventListener('change', function() {
let value;
if (this.checked) {
transition();
document.documentElement.setAttribute('data-theme', 'dark');
value = 'dark';
} else {
transition();
document.documentElement.setAttribute('data-theme', 'light');
value = 'light';
}
// save theme value
localStorage.setItem('data-theme', value);
});
// get previously applied theme
function setThemeOnLoad() {
let theme = localStorage.getItem('data-theme');
if (theme === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
// set toggle state
checkbox.checked = true;
} else {
document.documentElement.setAttribute('data-theme', 'light');
}
}
let transition = () => {
document.documentElement.classList.add('transition');
document.documentElement.addEventListener('animationend', () => {
this.classList.remove('transition');
});
}
And that's it. You're done.
Let me know in the comments if you have any doubt related to this.
Follow @creocodigo for more projects and web related content.
If you find this useful, below are some other posts that I am sure you'll love