Very Simple Light-Dark Theme Switcher With CSS Variables

Welcome to a quick tutorial on how to build a simple light-dark theme switcher using CSS variables. Looking to build your own switchable themes with CSS variables? Well, it really isn’t that difficult.

A common way to build switchable themes using CSS variables is to:

  • Create your HTML page as usual.
    • <header>Header</header>
    • <main>Content</main>
  • Set the “themeable components” to use CSS variables.
    • header { color: var(--txtcol); background: var(--headcol); }
    • main { color: var(--txtcol); background: var(--maincol); }
  • Lastly, use Javascript to change the CSS variables to set the colors and theme.
    • var doc = document.documentElement;
    • doc.style.setProperty("--txtcol", "black");
    • doc.style.setProperty("--headcol", "lightyellow");
    • doc.style.setProperty("--maincol", "white");

That covers the quick basics. Read on for an example of how we can use this to build a light-dark theme switcher!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Demo CSS Theme Useful Bits & Links
The End

 

DOWNLOAD & DEMO

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

DEMO – THEME USING CSS VARIABLES

 

 

LIGHT-DARK THEME WITH CSS VARIABLES

All right, let us now get into the example of creating a simple light-dark theme switcher with CSS variables.

 

STEP 1) THE HTML

1-theme.html
<!-- (A) HEADER -->
<header id="page-head">My Site</header>
 
<!-- (B) MAIN CONTENT -->
<main id="page-body">
  <input type="button" value="Light Theme" onclick="setTheme(0)"/>
  <input type="button" value="Dark Theme" onclick="setTheme(1)"/>
</main>
 
<!-- (C) FOOTER -->
<footer id="page-foot">Copyright, My Site.</footer>

This should be straightforward, there’s nothing “special” with the HTML. This is just a “regular” header, body, footer layout – Just take note of the 2 buttons that will be used to switch themes.

 

 

STEP 2) THE CSS

2-theme.css
/* (A) UNIVERSAL LAYOUT */
html, body {
  padding: 0;
  margin: 0;
  font-family: arial, sans-serif;
}
#page-head, #page-body, #page-foot { padding: 10px; }
 
/* (B) DEFAULT COLORS */
:root {
  --fontcolor: inherit;
  --headcolor: inherit;
  --bodycolor: inherit;
  --footcolor: inherit;
}
 
/* (C) CSS VARIABLES FOR COLOR SCHEME */
#page-head, #page-body, #page-foot { color: var(--fontcolor); }
#page-head { background-color: var(--headcolor); }
#page-body { background-color: var(--bodycolor); }
#page-foot { background-color: var(--footcolor); }

This is kind of over-simplified, but in your own project:

  • Don’t worry too much about “themes” first, start with your usual layout and styles.
  • Then determine which are the “switchable” features – Which are usually the colors, fonts, and backgrounds. Redefine these using CSS variables.
  • In this example, we have:
    • Switchable common font color --fontcolor.
    • Also background colors for each section --headcolor --bodycolor --footcolor.

 

 

STEP 3) THE JAVASCRIPT

3-theme.js
// (A) THEME COLORS
var themes = [
  // (A1) LIGHT THEME
  {
    "fontcolor" : "#333",
    "headcolor" : "#f0efff",
    "bodycolor" : "#fff",
    "footcolor" : "#efefef"
  },
 
  // (A2) DARK THEME
  {
    "fontcolor" : "#fff",
    "headcolor" : "#0a019d",
    "bodycolor" : "#343434",
    "footcolor" : "#343434"
}
];
 
// (B) SET THEME
function setTheme (t) {
  for (let i in themes[t]) {
    document.documentElement.style.setProperty("--"+i, themes[t][i]);
  }
}
 
// (C) USE LIGHT THEME BY DEFAULT
setTheme(0);

Remember the 2 “switch theme” buttons from earlier? The last part is to do the style switching in Javascript:

  1. We keep all the theme data (CSS variables and values) in the var themes nested array.
  2. This should be straightforward, setTheme() is fired when the user clicks on the switch theme button. We simply loop through the respective var themes object, and change the CSS variables.

That’s all to the mysteries of a CSS variable theme switcher.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Theme Using CSS Variable (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.