How to code a Theme Switcher with Vanilla JavaScript

Andy Leverenz
The Web-Crunch Column
2 min readFeb 15, 2019

--

originally posted at web-crunch.com

Welcome to another Let’s Build: with JavaScript tutorial. In this installment, we’ll take a look building a theme switcher for different parts of a web page using CSS variables and vanilla JavaScript.

Building a theme switcher has never been easier since the introduction of modern CSS variables. If you’ve been a Sass advocate you likely understand the point of a variable in your CSS code.

In my own work, I use variables to keep my code as DRY (don’t repeat yourself) and consistent as possible. With Sass, you can’t quite hook into the same code with JavaScript as you can with CSS variables.

Declaring variables on given elements in your regular CSS allows you to traverse the CSS for those handy variables. In this video guide, I’ll show you have to use some modern techniques in both JavaScript and CSS to achieve this awesome result.

View the codepen

Subscribe to my YouTube channel or newsletter to receive this content automatically

Want to learn Ruby on Rails from the ground up? Check out my upcoming course called Hello Rails.

--

--

Andy Leverenz
The Web-Crunch Column

Web-Crunch is a blog that delivers the crunchiest articles on design and development. Follow us or check out the website http://web-crunch.com