Guide on changing color schemes dynamically with SASS

Currently, I am exploring the implementation of switchable themes for a project, which in this case is similar to a Mendix project but suppose it's a regular website.

  • The website comprises various pages with different elements on each page.
  • All styles are coded in SASS. These pages and elements utilize values from a separate SASS file named variables-1.
  • There is also another SASS file available with the same variables but with distinct values - variables-2.

Issue:
My objective is to enable the color theme of the website to change upon clicking a button. Essentially, I want all the components to derive colors from a different set of variables once a button is clicked.


I have considered two possible courses of action:

  1. Compile 2 css files and alternate between them: link all sass files to variables-1, compile the CSS file, then link all sass files to variables-2, and compile the second css file. Switching between them is triggered by a button click.

    In this scenario, colors will not be dynamically assigned, and every time I need to make adjustments, I must manually link each sass file to varied variables or modify variable values before recompiling everything. Furthermore, I won't be able to seamlessly work on the project while switching between themes.

  2. Establish predefined color themes using mixins, produce css classes for each theme as outlined in this article, and apply custom JavaScript logic to assign appropriate classes to the elements.

    This approach most likely may not be practical due to the extensive number of elements in the project, making it overly complex to alter classes for each one.

Any suggestions on how to effectively tackle this challenge?

Answer №1

It is not feasible to dynamically modify sass variables in the browser. For a more thorough explanation, check out this resource:

Altering variable values scss

An effective workaround could be toggling classes on the body element to indicate different themes. For instance, if the body has the class "dark-theme", the background color would be black, and so forth.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I encountered a challenge with a cross-site scripting filter that is preventing me from storing a JavaScript variable in a MySQL database using a

I am currently working on a project where I need to save a JavaScript variable into a MySQL database using a Python CGI script. In this case, the JavaScript variable contains the following information: <li style = "width: 300px; background-color: white ...

Encountering a TypeError

Every time I try to submit a form request, I encounter an error [TypeError: object is not a function]. The problematic code lies within my hiren-conf.js file. Here's a snippet of the Mongoose code: var auth = require('../auth.js'); var mong ...

Overflow is not functioning as intended

Here is the CSS code I am using for the #Wall div: #Wall { z-index:0; position: absolute; left: 50%; top: 0px; margin-left: -952px; width: 1920px; height: 1200px; overflow: hidden; } This code creates a clickable wallpaper advertising image. However, ...

Retrieve all the records from the collection that have a specific reference number in their ID field

Is it feasible to pull together all documents with an ID that includes a specific value? I am working with Angular 7. I attempted using db.collection('items').where.. but unfortunately, this method is not supported. For instance: (collection) ...

What causes Express Async Errors to produce unexpected outcomes?

I stumbled upon a fantastic npm package called Express Async Errors that is highly recommended in the documentation. However, when I try to implement it, my server crashes. Here is my Route handler code: Controller const { Genre } = require("../models"); ...

How to apply bold styling to text with DOM Events

I am currently in the process of learning JavaScript and decided to practice by creating a text editor. The goal is to type something, click a button to change it to upper or lower case, bold, or italicize. While I successfully implemented the uppercase an ...

Updating to the latest version of Next.js will result in a modification of the API path

I recently updated my app to the latest version of nextjs (13.1.6) and encountered a problem where all my requests are now failing. It seems that the URL I specified in the request creator, based on the ENV value, is being overwritten. .env file CONST NEXT ...

Sending an AJAX request with an unpredictable quantity of parameters

I built a form for my website that pulls questions from a database based on certain variables. This means the number of questions available is unknown. The code snippet for displaying these questions looks like this: HTML <ol class="questions"> ...

What is the best way to selectively add multiple classes to a single Material UI classes attribute based on certain conditions?

I am trying to use the Material UI Drawer and wish to add a class named "paperStyle" to the classes prop with the rule name "paper" and then, under certain conditions, apply an additional class called "specialPaperStyle" for specific users. However, I have ...

Utilize the return function in a separate PHP file

I wanted to create a basic login form as a way to dive into learning php. After setting up the form, I wrote a function that sends a query to a MySQL database to fetch the username and password (stored in md5 encryption) for comparison with user input. T ...

Ordering Algorithm in AJAX Grid

I have a specific requirement that I spotted on this website: Whenever a user clicks on the table header, the content should be sorted accordingly. I have successfully implemented this feature for tables with pre-set values. https://i.sstatic.net/7cebx.p ...

Discovering every element on a webpage

Searching for elements in a webpage can be done using different methods. document.getElementsByTagName('*') and document.all Are there any other more efficient ways or are these two the most recommended? I am currently working on an element se ...

What is the best way to explain the concept of HTML5?

While reading a question on Stack Overflow, I came across a comment that stated: HTML5 is equal to HTML(5) + CSS3 + JavaScript. This statement truly caught me by surprise. Can it really be argued that HTML5 can be broken down into HTML(5), CSS3, and Ja ...

Use CSS media queries to swap out the map for an embedded image

Looking to make a change on my index page - swapping out a long Google Map for an embedded image version on mobile. The map displays fine on desktop, but on mobile it's too lengthy and makes scrolling difficult. I already adjusted the JS setting to "s ...

"Utilize tidymanaged to organize and structure your HTML code

I have implemented the Tidymanaged library to format HTML code. using (Document doc = Document.FromFile(AppPath + "/" + DefaultFileName)) { doc.ShowWarnings = false; doc.Quiet = true; ...

What is the alternative method to clicking right in Selenium without utilizing JavaScript and fireEvent()?

I am trying to perform a right click on a context menu in a Selenium test. I attempted to use fireEvent but was unsuccessful as I am not familiar with JavaScript. Can anyone provide guidance on how to successfully execute a right click in Selenium? Thank ...

Exploring the Depths of Angular: Uncovering Nested Objects

I'm having trouble locating a nested array within my Angular application. Specifically, I need to access the package.name property. Any assistance would be greatly appreciated. JSON [{ "id": 1, "name": "Yeah", "package_id": 1, "price ...

I am currently working on implementing a feature in my app that allows users to easily log in using their Google account

Currently, I am in the process of enhancing my app to enable users to log in using their Google account. The frontend is built with ReactJS, tailwindcss, and sanity. The login button successfully writes user data to the sanity database. However, I suspect ...

Sidebar height is not at 100% full height

I am using Bootstrap 3 and attempting to create a dual sidebar layout. Here's what I have so far: http://jsfiddle.net/Y52n9/11/ However, when you resize the preview screen to be large enough, you'll notice that the sidebar height is not reaching ...

pressing the button without physically interacting with it

Searching for information on my home page is simple – just input text in a textbox and click on the search button (Page 1). On another page, you'll find a similar setup with a textbox, a button, and an iframe that displays results upon clicking the ...