Sass-enhanced bespoke bootstrap themes that are easily switchable

While I came across various methods for implementing switchable themes in SASS, I could not quite grasp how to apply these techniques to overriding Bootstrap. Imagine wanting to have both light and dark themes that each override Bootstrap's colors differently.

// Light-theme
$theme-colors: (
    "primary": #8A2F4F
);

// Dark-theme
$theme-colors: (
    "primary": #fff
);

In the snippet above, you can see overrides for the primary color within the theme-colors. How do I go about conditionally setting them based on the theme chosen by the user?

Answer №1

Indeed,

There are several approaches you can take to achieve this. One method would be to create multiple CSS files based on the _variables.scss file, creating new themes with different variable sets. You can find an example of this technique in my public repository.

Another option is to utilize CSS custom variables, also known as custom properties. You could implement something like the following code snippet for reference:

In a Sass file:

$theme-colors: (
    "primary": var(--primary-color);
);

In another variable file:

element.dark {
  --primary-color: black;
} 
element.light {
  --primary-color: white;
} 

These are two methods that I would recommend exploring.

Answer №2

Here is the technique I used with React, Bootstrap, and SASS:

Inside an index.scss file:

div#blue {
  @import './blue.scss';
}

div#red {
  @import './red.scss';
}

// Including the rest of Bootstrap
// It seems necessary to import twice for fonts to work properly
@import "node_modules/bootstrap/scss/bootstrap";

Then in the red.scss file, I customized Bootstrap as needed:

$theme-colors: (
        "primary": red,
);

// Importing the rest of Bootstrap
@import "node_modules/bootstrap/scss/bootstrap";

The blue.scss file follows a similar structure.

Finally, let's look at my top-level React component:

import './index.css'
import * as React from 'react'
import Button from 'react-bootstrap/Button'

export const Container = () => {

const [color, setColor] = useState('red');

const buttonClick = () => {
    if (color === 'red') setColor('blue')
    if (color === 'blue') setColor('red')
}

return (
        <div id={theme} >
            Switch themes...
            <Button variant="primary" onClick={buttonClick}>Button</Button>
        </div>
    )
}

By clicking the button, the color will toggle between red and blue. While using a className over a div selector may be more advisable, this approach has been effective for me so far. Its robustness in complex applications remains to be fully determined.

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

Spread out a div across a container's width

Currently, I am struggling to modify my css and html in order to have a div expand horizontally into a scrollable div. However, all I seem to get is the content stacking once the width limit is reached. Take a look at this fiddle for reference. The absol ...

alignment of text output and label that responds to various screensizes

Here is some html code with a label and an output next to it. <div class="panel-body"> <div class="col-sm-6"> <label class="head6">Company Name : </label><span class="head9 halfR"> ...

Troubleshooting: Jquery show effects not functioning as expected

Currently, I am facing an issue where I am attempting to display a fixed div using the show function of jQuery. Although the show function itself is working properly, when I try to include an effect from jQuery UI, it does not seem to work as expected. Bot ...

Tips for keeping table row heights consistent while scrolling in Firefox

I am dealing with an issue on my HTML table where the headers are static and the rest of the body scrolls within a fixed size window. The problem arises when the number of rows is less than what fits in the scrolling window, causing the row size to automat ...

Tips for keeping your chosen CSS style sheet consistent when moving around the site

I am currently in the process of creating my first website. I have successfully added two buttons that allow users to switch between a light theme and dark theme, with the light theme being the default. However, I am encountering an issue that I cannot see ...

Is it possible to have the cursor rotate or animate by 45 degrees when clicked

Do you know how to create a unique custom cursor using CSS? Say, for example, we have this code: cursor: url(images/cursor.png) 15 15, auto; Now, what if we wanted to take it up a notch and make the cursor rotate -45 degrees when clicked, and then revert ...

The grid images transition at set intervals using jQuery or another JavaScript framework

I am facing a challenge with developing an image grid that transitions some images at random intervals using either jQuery or another method in JavaScript. It's important to note that I don't want all the images to change simultaneously; each gro ...

View the picture directly on this page

Currently, I am in the process of creating a gallery and I would like the images to open on top of everything in their original size when clicked by the user. My expertise lies in HTML and CSS at the moment, but I am open to learning JavaScript and jQuery ...

Using Node-sass to compile several input files

Within my project, I am utilizing node-sass directly through a command-line interface, specifically within a Makefile, to compile my CSS files. Currently, the scss files reside in separate directories and do not reference each other. Ideally, I want to co ...

Looking to change the font text color in the filter section of the react-data-table-component-extensions?

Can anyone offer some assistance with this issue? I am currently using react-data-table-component along with react-data-table-component-extensions. I have also imported the index.css for the component-extension. import DataTable from "react-data-tabl ...

Animating UL/LI elements using the power of CSS3 transformations

I am interested in creating animations using CSS3 transitions. Let's consider the following UL/LI element: <ul> <li class="green" id="green" style="display: none;">Contents 1</li> <li class="red" id="red" style="display: ...

Display issue with alert box

I run a website where users can submit posts and receive alert messages (success or warning) when they do so. The alert messages are displayed at the top of the page using position: absolute to prevent them from affecting the layout when they appear. Every ...

ckeditor ruined my header section

My website has a header that stays fixed at the top, much like Facebook. https://i.sstatic.net/sQOCG.png Unfortunately, when I scroll down, the CKEditor toolbox ends up overlapping and disrupting my fixed header. https://i.sstatic.net/RxFJ9.png ...

Unifying hoverlabels in Plotly: A guide to editing them

While working on generating a graph with two contour lines, I encountered an issue with the hover menu that appears when hovering over the graph. The menu displays values of the two contours as shown in the image below: https://i.sstatic.net/oSQex.png Th ...

Tips for eliminating flicker upon the initial loading of a webpage caused by dynamic changes in CSS styles through JavaScript

Struggling with the load order of my CSS and JavaScript... Currently, I have a div with a blue background that is styled to appear sliced diagonally. However, upon initial page load (or CTRL+SHIFT+R), there's a brief moment where the slice effect doe ...

Ultimate Solution - Next Level jQuery Mega Menu

I've been on the hunt for a way to create a mega menu similar to Mashable's without relying on PHP or MySQL, and I'm hitting a roadblock. If anyone has any insight, it would be greatly appreciated. I feel like I've been staring at my s ...

The menu bar fails to maintain its responsiveness once the breakpoint is reached

The issue I am facing is that the menu bar does not resize properly when the browser window size is less than 900px. This results in the bar being too wide, some content going off the screen, and a horizontal scrollbar appearing. I have been trying to tro ...

Troubleshooting a position: fixed conflict in CSS or browser caused by a jQuery script

Check out this CodePen example. Go to the tab block by scrolling down. When you scroll to the top, it adds the 'sticked' class, and when it reaches the bottom of the parent element, it gets the 'sticked2' class. The problem arises when ...

If the AJAX call is successful, proceed with the desired action

Currently, I am utilizing an AJAX call to retrieve data from a PHP file in JSON format. My goal is to change the color of my element based on whether a certain part of the data is true. I attempted to implement it as suggested here (async:false): Submit f ...

Tracking when jquery (bootstrap) has finished updating the page in Python Selenium using Ajax. Time to capture a screenshot!

I am facing an issue where my screenshot is being taken before jQuery has finished loading completely. While the page loads fine in IE, Edge and Firefox, sometimes the execution of jQuery is a bit slow which results in a transitional image. start_time = ti ...