Theme Switcher With CSS Variables – Very Simple Example

Welcome to a quick tutorial on how to build a 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:

  1. Build the page layout and default theme as usual.
  2. Switch the “skinnable” components to use CSS variables. For example, the font and background colors.
    • :root { --txtcol: black; --backcol: white; }
    • body { color: var(--txtcol); background-color: var(--backcol); }
  3. Lastly, use Javascript to change the CSS variables and theme.
    • var doc = document.documentElement;
    • doc.style.setProperty("--txtcol", "red");
    • doc.style.setProperty("--backcol", "lightyellow");

That covers the quick basics, but read on for a detailed example!

ⓘ 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.

 

 

REAL 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.

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

DEMO – THEME USING CSS VARIABLES

 

 

THEME WITH CSS VARIABLES

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

 

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.

 

 

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 a switchable common font color --fontcolor, and switchable background colors for each section --headcolor --bodycolor --footcolor.

 

 

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" : "#4e45de",
    "bodycolor" : "#dcdcdc",
    "footcolor" : "#4c4c4c"
  }
];
 
// (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:

  • A – We keep all the theme data (CSS variables and values) in the var themes nested array.
  • B – 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.

 

INFOGRAPHIC CHEAT SHEET

Theme Using CSS Variable (Click To Enlarge)

 

LINKS & REFERENCES

 

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. Required fields are marked *