How to Build a Simple Theme Switcher With the CSS Checkbox Hack

In this tutorial, you’ll learn how to create a CSS-only theme switcher by taking advantage of the “CSS checkbox hack technique” and CSS variables.

Ready for another CSS challenge?

Note: This tutorial assumes that you’re familiar with this particular CSS technique along with advanced CSS selectors (e.g. general sibling combinator). If you haven’t heard of these before, or need a refresher, check out the resources at the end of this tutorial.

Our CSS Theme Switcher

Here’s the theme switcher that we’re going to create during this tutorial:

Click on any of the colors at the top right corner to see how the page appearance changes. You can also use the arrow keys of your keyboard to switch color schemes.

1. Begin With the HTML Markup

We’ll start with eight radio buttons that will represent the available theme colors. By default, the first radio button will be checked.

Then, we’ll define a wrapper element that will control the page colors. Inside it, we’ll place:

  • The associated labels for these radio buttons and
  • the page’s main content. This page will present a few things about 🐧🐧.

Here’s the page structure:

2. Define Some Basic Styles

As usual, we’ll continue with some common reset styles.

Most importantly, pay attention to the following things:

  • We’ll use custom properties to define eight different color schemes. Each scheme will include two color definitions that will represent the page colors for a specific theme.
  • We’ll visually hide the radio buttons by moving them off-screen. We’ll give them position: fixed because, in the layout, the color palette should remain fixed as we scroll. Keep in mind, that if we give them position: absolute, each time we click on a theme color, the browser will jump to the top of the page.

Here are the reset styles:

3. Set the Main Styles

Moving forward, we’ll set up the main styles.

Most importantly:

  • As described above, the color palette will always stay in the top right corner as we scroll.
  • Each label/theme color will look like a circle, and its color will depend on the value of the *-bg-color CSS variable of the related color scheme.
  • We’ll add a bit of transitioning when the page colors change.

Here are the crucial styles:

4. Checkbox Hack: Switch Between Themes

Now for the interesting part! Each time a label/theme color is clicked (i.e. becomes active), the following things will happen:

  • The ::before pseudo-element of the active label will appear. Additionally, its color will depend on the value of the *-text-color CSS variable of the related color scheme.

The active color theme

  • The page colors will smoothly change based on the active color scheme.

Here are the related styles:

It might take you some time to understand how the aforementioned styles work. If this is the case, the browser inspector along with the other similar tutorials are your best friend!


That’s it, folks! In this short tutorial, we combined the power of the “CSS checkbox hack technique” and CSS variables to build a page with dynamic color themes. Hopefully, you enjoyed this exercise and you’ll try it in one of your upcoming projects.

Here’s a reminder of what we built:

If you also want to build a more advanced theme switch that persists the selected theme color on page load, be sure to read this tutorial.

Have you ever developed  something similar with pure CSS for a project? Let us know via social media. As always, thanks for reading!

Further Reading

Check out these tutorials to learn more about things you can build with the CSS checkbox hack technique:

Leave a comment

Your email address will not be published.