Customize the primary color in Bootstrap 4 using SCSS by overriding it based on the base class

I am in the process of developing a multi-site application using a single framework and incorporating Bootstrap 4 with Webpack.

My goal is to dynamically adjust certain variables of Bootstrap based on the base class applied to the body element. For example:

.class1{
    $theme-colours: (
        primary: red,
    );
 }

.class2{
    $theme-colours: (
        primary: blue,
    );
 }

Subsequently:

<body class="class1">
    <h1 class="text-primary">RED TEXT</h1> 
</body>

And:

<body class="class2">
    <h1 class="text-primary">BLUE TEXT</h1> 
</body>

Is this achievable in SASS? I have attempted to utilize mixins, but haven't had any success...

Answer №1

To create custom themes in Bootstrap 4, you can redefine the -primary classes using the color functions and mixins provided by Bootstrap. By putting the primary color changes into a custom @mixin, you can easily build each primary "theme":

@mixin build-primary-classes($color) {
    @include bg-variant(".bg-primary", $color);
    
    .btn-primary {
        @include button-variant($color, $color);
    }
  
    .btn-outline-primary {
        @include button-outline-variant($color);
    }
    
    @include text-emphasis-variant(".text-primary", $color);
    
    .border-primary {
        border-color: $color !important;
    }
}

.theme1 {
    @include build-primary-classes($purple);
}

.theme2 {
    @include build-primary-classes($red);
}

Check out the code snippet here

For more flexibility, you can store your custom theme colors in a map and generate theme classes dynamically:

$customthemes: (
  "theme1": purple,
  "theme2": cyan,
  "theme3": orange
);

@each $themeName, $color in $customthemes {
  .#{$themeName} {
    @include build-primary-classes($color);
  }
}

Explore the dynamic theme generation here


If you're using Webpack, you can pass the $customthemes map to the extract-text-webpack-plugin and SASS-loader for efficient styling. Simply include the $customthemes data using the 'data' option:

webpack-config:

     ....
     use: extractSass.extract({
         use: [
                  {
                    loader: "sass-loader",
                    options: {
                      allChunks: true,
                      data: '$customthemes:("theme1":purple,"theme2":pink,"theme3":red);'
                    }
                }]
         })
      .....

Keep in mind that having too many custom themes may lead to large CSS files. Consider generating separate theme.css files with SASS and including them as needed in the HTML HEAD section.

Related Resources:
How to customize Bootstrap 4 with SASS
Changing the Bootstrap primary color

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

Stylesheets from node_modules cannot be imported without using the tilde (~) character

Struggling to develop a basic web app using material-components-vue alongside vue-cli and webpack, I encountered an issue with importing stylesheets from node_modules without prefixing them with ~. After experimenting with various webpack/vue-cli configur ...

What is the best way to remove a global npm package?

I initially set up webpack by using the following command: npm install -g webpack Now, I need to remove it: npm uninstall -g webpack However, when I check for the version, it still shows: webpack -v 3.1.0 Why is this happening? I also tried searchin ...

What's causing margin-top to be reversed while margin-bottom remains stationary?

I have encountered a specific issue that I can't seem to find a solution for. I am attempting to include the text "2017 Indie" at the bottom of the page. When I apply margin-top to a pixel value, the text remains unchanged and nothing happens. However ...

What is the best method to position this div above the other div?

I am facing a challenge in positioning one div above another in Bootstrap. Inside a parent div, I have two child divs - one with an image and the other with text. Despite trying various methods like adjusting margins using CSS, I cannot seem to get the de ...

What is preventing the two spans from being centered within the div?

I'm having trouble centering the two spans within the div. How can I fix this issue? The code below is not working, but when I move the div outside of the two spans and change the display property to inline-block, it works. Why is this happening? b ...

Position the Bootstrap button on the right side of the navigation bar

Bootstrap is something I am currently exploring, and I am facing a challenge with aligning the button to the right within my navbar. <nav class="navbar navbar-light bg-light"> <button type="button" class="btn container ...

Displaying an image with a JavaScript variable is a common task in web

I have a Javascript code snippet below where the image name "samson decosta" is stored in a MySQL database. I am retrieving this image and trying to display it as a background_image in a div. document.getElementById("image_chk").style.backgroundImage="url ...

Appearing text dialogue

Can you tell me the name of the box that appears on top of a webpage? A couple of examples include: Facebook photos - It dims the background webpage and opens a separate pop-up to display pictures. Advertisements - The box that typically requires clickin ...

Altering the appearance of the Bootstrap navigation bar icon

My website's bootstrap navbar hamburger icon is too small for visitors to see. I tried adjusting the size, but had no success. Does anyone have any suggestions on how to solve this issue? For reference, I am using the most recent versions of bootstra ...

Flexbox allows you to easily manage the layout of your website

I am currently working on a CSS project and have encountered an issue. Whenever I apply the "display: flex" property to the .student element, the border around it mysteriously doubles. The reason for wanting to use the flex property is to align the text ve ...

Building an accordion collapse feature in HTML using CSS and JavaScript

I've been working on creating an accordion interface, but I'm facing an issue where only the first collapsible button works properly. The rest of the buttons are not functioning and don't even appear on the page. When I remove the CSS styli ...

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

Utilizing the box-shadow feature on Bootstrap 4's card element

I'm having an issue with applying a simple shadow to a cards component. The shadow appears mirrored and out of place. My suspicion is that it might be related to the margin-bottom property, but I'm unable to pinpoint the exact reason behind this ...

What is the reason behind the incompatibility of css background-size/position with the shorthand background url?

I had been working on a concept for a slideshow or single-page website and reached a good stopping point. Since I had outlined my idea on a JSFIDDLE, I decided to tidy up my CSS. Each of the 5 tabs used similar lines of CSS except for the background image. ...

Click on the shadowed div element to interact: inset

Is there a way to make elements clickable/focusable within a div that has an inset shadow, like the .calendar in this example? Check out this code snippet: https://jsfiddle.net/axel50397/846ostv5/9/ <div class="calendar"> <div class="card-dec ...

How can I expand and collapse all the Bootstrap panels simultaneously?

Is it possible to make all three panels collapse and expand simultaneously with just one button? <button data-toggle="collapse" data-target="#collapseOne-1,#collapseTwo-1,#collapseThree-1" ...></button> <div id="#collapseOne-1" class="coll ...

Failed to load resource: The server returned a 404 error when deploying React on Github Pages

Currently working on a React App. Started by deploying on GH page with SASS, but encountered an issue. Can anyone provide guidance on resolving this? Unsure of the specific code needed, so just looking for which part of code is necessary to add. https:/ ...

Is it necessary for 'ts-loader' to have 'typescript' installed in order to function properly? I personally did not encounter any issues with this

The npm documentation for using ts-loader suggests installing typescript. The official Typescript guide in webpack documentation also recommends the same, but without providing a clear explanation. However, I have successfully built everything without havi ...

When the text exceeds its container, the CSS property overflow will display only the

.example{ overflow:auto; } <div class="example"> <p>Some Additional Text Here<P> </div> This particular code snippet showcases the initial portion of the text, allowing for scrolling down to reveal items 5 and 6: 1 2 ...

Issue with fancyBox not functioning properly when integrated with a caption script

Hey there! I've been working with both jQuery's fancyBox for displaying image/video galleries and the Capty script for showing image titles on images. However, I've run into a snag - when the title is displayed on the image, fancyBox st ...