Detailed explanation of how to easily switch CSS themes

Detailed explanation of how to easily switch CSS themes

I recently added a very simple color scheme (theme) switcher to my personal website. You can toggle this simple color switcher in your website’s footer to see it in action. In case anyone else is looking to add functionality like this to their own site/project, I thought I'd write a quick post explaining how to do it. Let’s get started.

HTML

First, we need to include the "buttons" that will trigger the theme to switch based on the theme selected. (Note: you could always make these as options in a select element if that's better for you)

<div class="color-select">
    <button onclick="toggleDefaultTheme()"></button>
    <button onclick="toggleSecondTheme()"></button>
    <button onclick="toggleThirdTheme()"></button>
</div>

That's it! Don't worry too much about the onclick parameter for now, we'll come back to this when we add the JavaScript. The only item left is to add the default theme class to html element like this:

<html class="theme-default">

CSS

Next, we need to style the two color-select buttons with the custom color scheme that will change throughout the site. We'll start with the color scheme.

To make switching between these themes seamless, we’ll set the changing color sets as CSS variables:

.theme-default {
   --accent-color: #72f1b8;
   --font-color: #34294f;
}

.theme-second {
    --accent-color: #FFBF00;
    --font-color: #59316B;
}

.theme-third {
    --accent-color: #d9455f;
    --font-color: #303960;
}

body {
    background-color: var(--accent-color);
    color: var(--font-color);
}

Finally, we style the user-facing color palette:

.color-select button {
    -moz-appearance: none;
    appearance: none;
    border: 2px solid;
    border-radius: 9999px;
    cursor: pointer;
    height: 20px;
    margin: 0 0.8rem 0.8rem 0;
    outline: 0;
    width: 20px;
}

/* Style each swatch to match the corresponding theme */
.color-select button:nth-child(1) { background: #72f1b8; border-color: #34294f; }
.color-select button:nth-child(2) { background: #FFBF00; border-color: #59316B; }
.color-select button:nth-child(3) { background: #d9455f; border-color: #303960; }

JavaScript

We need to make each of the swatch buttons trigger its corresponding theme and swap out theme-default class that we originally attached to the main html element. We also need to store what the user selected localStorage , so when they reload or navigate to a different page, their selection persists.

// Set a given theme/color-scheme
function setTheme(themeName) {
    localStorage.setItem('theme', themeName);
    document.documentElement.className = themeName;
}

// Toggle between color themes
function toggleDefaultTheme() {
    if (localStorage.getItem('theme') !== 'theme-default'){
        setTheme('theme-default');
    }
}
function toggleSecondTheme() {
    if (localStorage.getItem('theme') !== 'theme-second'){
        setTheme('theme-second');
    }
}
function toggleThirdTheme() {
    if (localStorage.getItem('theme') !== 'theme-third'){
        setTheme('theme-third');
    }
}

// Immediately set the theme on initial load
(function () {
    if (localStorage.getItem('theme') === 'theme-default') {
        setTheme('theme-default');
    }
    if (localStorage.getItem('theme') === 'theme-second') {
        setTheme('theme-second');
    }
    if (localStorage.getItem('theme') === 'theme-third') {
        setTheme('theme-third');
    }
})();

That's it! Now, it just depends on how customized you want each theme style to be. The possibilities are endless!

This concludes this article on how to easily switch CSS themes. For more information about switching CSS themes, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  In-depth explanation of MySQL isolation level and locking mechanism

>>:  Implementing login page based on layui

Recommend

A brief introduction to VUE uni-app basic components

1. scroll-view When using vertical scrolling, you...

Detailed explanation of JavaScript BOM composition and common events

Table of contents 1. BOM 2. Composition of BOM 2....

Comparative Analysis of UI Applications of Image Social Networking Sites (Figure)

In our life, work and study, social networks have ...

Solution to mysql login warning problem

1. Introduction When we log in to MySQL, we often...

DOCTYPE Document Type Declaration (Must-Read for Web Page Lovers)

DOCTYPE DECLARATION At the top of every page you w...

Detailed explanation of Zabbix installation and deployment practices

Preface Zabbix is ​​one of the most mainstream op...

How to run py files directly in linux

1. First create the file (cd to the directory whe...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

No-nonsense quick start React routing development

Install Enter the following command to install it...

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

Zen Coding Easy and fast HTML writing

Zen Coding It is a text editor plugin. In a text ...

Example of how to import nginx logs into elasticsearch

The nginx logs are collected by filebeat and pass...

Detailed explanation of mysql backup and recovery

Preface: The previous articles introduced the usa...

A brief discussion on how to learn JS step by step

Table of contents Overview 1. Clearly understand ...