Can SCSS be used to switch themes on a website?

I am looking to implement a dynamic theme change feature when the user clicks on a specific button.

When users click on a checkbox (checkbox is checked)

The following commented theme should be applied:

/** 
  $theme1-background:white;
  $theme1-font-color:black;
  $theme1-btn-primary:rgba(243,241,0,0.1);
  $theme1-btn-secondary:#2ab1e4;
**/

If the checkbox is not checked, then the default theme should be applied.

I have come across this functionality in many applications but I'm unsure how to implement it myself.

For reference, here is the complete codepen link: https://codepen.io/eabangalore/pen/XPqoBK

$theme1-background:rgba(0,0,0,0.8);
$theme1-font-color:white;
$theme1-btn-primary:green;
$theme1-btn-secondary:orange;

//below setting has to be applied based on theme 2

/** 
  $theme1-background:white;
  $theme1-font-color:black;
  $theme1-btn-primary:rgba(243,241,0,0.1);
  $theme1-btn-secondary:#2ab1e4;
**/


.main{
  margin-top:34px;
  background:$theme1-background;
  border:1px solid orangered;
  width:90%;
  color:$theme1-font-color;
  .btn-primary{
    background:$theme1-btn-primary;
    color:$theme1-font-color;
  }
 .btn-secondary{
    background:$theme1-btn-secondary;
    color:$theme1-font-color;
  }
}
<label>change theme:<input type="checkbox"></label>

<div class="main">
  <button class="btn-primary">button primary</button>
  <button class="btn-secondary">button secondary</button>
  
  <p>text color   ------>>>  Lorem, ipsum dolor sit amet consectetur adipisicing elit. Iure delectus officiis ea in deserunt blanditiis at, ratione recusandae asperiores pariatur perspiciatis voluptate accusantium aperiam, harum accusamus quis veritatis quisquam aliquid.</p>
</div>

Answer №1

If you want to dynamically change the style of an entire page, there are two simple steps you can follow:

Start by creating a duplicate stylesheet and include a body class selector like body.other-theme at the beginning of each selector you wish to modify. Your two stylesheets might look something like this:

/* main-theme.css */
#content {
  background: white;
}

/* other-theme.css */
body.other-theme #content {
  background: black;
}

Then, when the user toggles the checkbox, just add the other-theme class to the document's body. This will activate all styles associated with the "other-theme."

In each stylesheet, you can define theme colors and other variables specific to that particular theme.

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

Position the div to the right of a left navigation bar instead of beneath it using Bootstrap 5

After using the code snippet from , I'm struggling to align content to the right of the navigation bar. It seems like a simple issue related to changes in Bootstrap 5 that I can't seem to figure out. <div class="d-flex flex-column vh-100 ...

Using Python with Selenium, attempt to click on a button designated as type="button"

I've been trying to click on a button in this Airbnb listing but none of the codes I tried seem to work. Here is one example of the HTML code: <li data-id="page-2" class="_1eqazlr"> <button type="button" class="_1ip5u88" aria-label="Page 2 ...

What could be causing the HTML5 video to not function properly on the latest Safari version for Windows?

After conducting tests on multiple installations of Safari 5.1.7 for Windows, I am having trouble getting HTML5 videos to play. I have been using the following link for testing: Basic HTML5 video test The videos appear to work fine on Safari 5.1.7 for Ma ...

Wait for the jQuery UI tabs to load until the entire HTML has been rendered

I have implemented jQuery UI tabs on my website. Occasionally, when the page first loads, all the HTML content for each tab is visible before the tabs themselves appear. This causes a slight delay in positioning the content within the tabs and creates an ...

Avoid changing the styling of the parent element when hovering over it

I am working on a comment box that allows for nested comments and is structured in the following way: <article class="comment"> Level 1 comment <article class="comment"> Level 2 comment </article> <article class="comment"& ...

Centered Fixed Upward Scrolling Div

Currently facing a challenge with my project involving a chat interface created in Angular. I am struggling with the CSS styling as the chat starts at the top and scrolls downward off-screen as the conversation progresses. Although I can scroll along with ...

Slideshow not displaying properly after an Ajax request due to CSS not being applied

My goal is to dynamically load data when reaching the bottom of a page using the onScroll event handler. The data to be loaded mainly consists of slideshows and, behind each one, within a separate div, there is an iframe with embedded vimeo content. Initi ...

Angular ngStyle Parsing Issue: Encountered an unexpected symbol [ when an identifier or keyword was expected

Is there a way to pass a dynamic string using the fieldName to retrieve an attribute from the item object without encountering syntax errors in older versions of Angular? Here is a simple example to illustrate the issue. While this code works fine with ang ...

Numerous sections of content

Struggling to align these two pieces of content side by side. While I've had success displaying content like this in the past, I'm hitting a roadblock this time around. Any assistance would be greatly appreciated. HTML <div class="block-one" ...

Tips for eliminating white frames or borders on the Mapbox canvas map

In my current project using Angular 10, I have integrated Mapbox to display path routes. Following the standard Angular practice of splitting components, I separated the map rendering component and called it into the main map component. However, I encounte ...

Troubleshooting problem with SVG icons not highlighting correctly when clicked on mobile devices

I recently incorporated SVG icons into my website. However, when I tap on them using a mobile browser, the browser highlights the icon. Is there a way to disable this highlighting effect? Example: highlight Code: <div class="ictg-before"> <svg ...

Tips for inserting a PHP variable into a Bootstrap CCS class to change the background image dynamically

I currently have CSS code set up for a full background image. I am now looking to dynamically change the background image by passing a PHP variable to the URL. Can someone please help me with this? Here is my current code: .imgback { padding-top:140px; ...

What is the best method for fetching data from PHP to display on my Angular page?

I am struggling to retrieve data from MySQL to display on my AngularJS page, using PHP as my server-side language. I can fetch the data from my PHP file but unable to render it on my page. If you have any alternative solutions, please let me know. Below i ...

Converting HTML and CSS to PDF in Java using iText

I've been working on converting HTML to PDF. Initially, I transformed my HTML code into XHTML using the resources provided in this link: After successfully displaying the generated XHTML code in a browser by creating an HTML file for testing purposes ...

Ensure that the Bootstrap 5 modal and backdrop are sized to fit the parent container's width and height rather than filling the entire screen

Is there a way to limit the size of a Bootstrap 5 modal dialog and backdrop? The example below occupies 100% of the screen, can it be adjusted to cover only the parent main container? <main class="container-fluid pt-4 px-4" style="height ...

How can one efficiently manage a large number of images on a website in a clever way?

I have a straightforward webpage with a large number of images. Just like this... <img src="img/photo00001.jpg"> <img src="img/photo00002.jpg"> <img src="img/photo00003.jpg"> ... and so forth I don't want to waste hours copying and ...

I am interested in shuffling the letters within words that I receive from the input in my textarea

Is there a way to modify some of the letters in certain words entered into a textarea without changing the order of the words? I'm looking to shuffle around the letters within specific words rather than scrambling the entire sentence. I considered ut ...

Detect keyup event within a dynamically injected iframe

I am currently using a text editor that utilizes wysihtml5. I suspect the text editor is embedded within an iframe. My goal is to capture the key up event inside the text editor and prevent scrolling within it. Below is the code snippet that loads the tex ...

What is the best way to make a full width div within a container using bootstrap?

Imagine you have the following code snippet: <div class="container"> <div class="row"> <div class="col-md-12"> ... <div class="full-width-div"> </div> </div> & ...

Unable to retrieve JSON data at this time

I am currently attempting to retrieve information from a JSON file using JQuery in my HTML code with the following script: $.getJSON( "en.json", function( data ) { console.log(data.age); }); Unfortunately, it doesn't seem to be working as expe ...