Switching theme of my website

Johan Hjalmarsson

I wish to allow users to switch the entire color-scheme of my site with a buttonpress.

I have 2 separate .less files with the same global variables, but different colors. The problem seems quite simple.. "swap 1 .less file for the other". But how do I actually accomplish this?

Of course, I could alter the entire site element-by-element in the .js based on the state of a singleton, but that seems like a duct-tape-solution.

Benny

One possible solution by example: say you have a div element that you wish to color its background differently in each theme. Load both .less files and edit them like this (add the theme class to the body element that wraps the whole document):

theme-1.less

body.theme-1 {
    div.address {
        background-color: yellow;
    }
}

theme-2.less

body.theme-2 {
    div.address {
        background-color: fuchsia;
    }
}

Then provide a mechanism for the user to change the theme (e.g. drop-down menu). When the user selects from the menu - say for example changing from theme-1 to theme-2 - issue this JS:

document.getElementsByTagName("body")[0].classList.replace("theme-1", "theme-2");

Generically you can do it in more than one way:

document.getElementsByTagName("body")[0].classList.replace(<currently-selected-theme>, <newly-selected-theme);

or

document.getElementsByTagName("body")[0].classList.remove(<currently-selected-theme>).add(<newly-selected-theme>);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Turning my current website configuration into a wordpress theme

how to embed youtube live chat page with dark theme in my website

How can my website detect if a browser is using a dark/light theme NOT if the OS is using a theme

Flutter - Switching images depending on Theme

Switching theme by button Codename One

Appearance does not change on switching the theme

Navbar theme switching not working as expected

How do I attach an SSL certificate to my asp dot net website without switching to IIS?

I have a button that switches the theme of my website how do i make the same button if clicked again to change back to the default Theme?

How exactly do I create a new, separate page to my website in a Shopify theme?

How can I remove the Theme owner and Powered by wordpress from my website?

Switching from website to instant app

how to add animation for theme switching in flutter?

Transition when theme switching (or Activity.recreate())

How to consider variation while switching theme?

change color BorderSide when switching to dark theme

Switching Plasma theme from the command line

Switching Wordpress theme live without losing functionality

My wordpress theme error

Switching website into persistent full screen mode with a button

switching wordpress website from test to live in plesk

Why is my array switching values?

How to edit the Simplex Theme for a Quarto Website?

Can you find the wordpress theme of a website?

Entire viewport goes dark after switching to dark theme?

Conditially override scss variables for bootstrap 4 theme switching

Delphi VCL application layout changes when switching theme

Switching theme with ThemProvider causes the widget to reset its state

Converting my Application Theme to Lollipop