In this simple tutorial you are going to learn how to implement a theme switcher for your website using CSS and Javascript.
Let's start by creating a simple HTML structure.
And a simple CSS to start, just to prepare the page.
Now we are going to create a button that will trigger the theme.
We can start with a dark-theme button and a script that will include a data-theme attribute to the body element.
Now we have to implement the themes.
we start by creating the :root variables.
Let's also include other themes
You should now be able to see the buttons and switch to the selected theme, but the theme is reset to the default theme when reloading the page. In the next section we will store that selected theme on localStorage.
Storing theme on localStorage
Now let's store the selected theme, so we can keep the style even if the user reload the page.
That's it.
Now challenge for you 🤔.
Implement a toggle switcher from light to dark theme using only one button. You can use emoji to identify the states ☀️ and 🌙.