Having trouble with modifying Bootstrap Sass variables when importing specific Bootstrap components?

I am facing a challenge in grasping the concept of overriding or adding default variables without importing the entire Bootstrap library. The documentation for Bootstrap suggests two options: either importing everything or just the specific parts you require.

First option is to import all of Bootstrap:

// Custom.scss
// Option A: Include all of Bootstrap

// Define any default variable overrides here (functions won't be accessible)

@import "../node_modules/bootstrap/scss/bootstrap";

// Add any custom code here

The second option is to import only what you need.

// Custom.scss
// Option B: Include parts of Bootstrap

// 1. Start by including functions first (to manipulate colors, SVGs, calc, etc)
@import "../node_modules/bootstrap/scss/functions";

// 2. Define any default variable overrides here

// 3. Proceed to include the rest of required Bootstrap stylesheets
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

// 4. Include optional Bootstrap components as necessary
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/containers";
@import "../node_modules/bootstrap/scss/grid";

// 5. Proceed with additional custom code here

The issue arises when attempting to override or add variables using the second option like this:

// Custom.scss
// Option B: Include parts of Bootstrap

// 1. Include functions first (to manipulate colors, SVGs, calc, etc)
@import "../node_modules/bootstrap/scss/functions";

// 2. Define any default variable overrides here

// Attempting to add extra spacer sizes
$spacer: 1rem;
$spacers: (
  6: ($spacer * 5)
);

// 3. Include remainder of required Bootstrap stylesheets
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

// 4. Include any optional Bootstrap components as needed
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/containers";
@import "../node_modules/bootstrap/scss/grid";

// 5. Proceed with additional custom code here

This approach does not yield the desired results. Alternatively, following the same process but with the first option, like so:

// Custom.scss
// Option A: Include all of Bootstrap

// Define any default variable overrides here (although functions are not available)
$spacer: 1rem;
$spacers: (
  6: ($spacer * 5)
);

@import "../node_modules/bootstrap/scss/bootstrap";

// Include any custom code here

This method works, however, it involves importing the whole of Bootstrap. I am eager to discover how to achieve the second option--importing only necessary components while still having the flexibility to override variables. Please feel free to use the spacer example I mentioned if you have a solution.

Answer №1

If you're utilizing Bootstrap 5 as indicated in the question's documentation, updating the $spacers map and rebuilding all utility classes requires importing "utilities" and "utilities/api"...

@import "functions";
@import "variables";
@import "mixins";

$spacers: map-merge(
    $spacers, (
        6: $spacer*5,
        7: $spacer*6,
    )
);

@import "utilities";
@import "root";
@import "reboot";
@import "type";
@import "images";
@import "containers";
@import "grid";
@import "utilities/api";

View Demo on Codeply

This method allows for selective importing of specific components.

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

What is the best way to ensure that a sidebar occupies the entire height of our webpage?

Here is the current layout of my sidebar: https://i.sstatic.net/c1sy6.png I want it to always occupy full height, how can I achieve this? I've tried using height: 100%, but it doesn't seem to work. Here is my CSS code: #sidebar { /* Make s ...

Ways to condense a text by using dots in the middle portion of it

How can I dynamically shorten the text within a container that varies in width? The goal is to replace the strings between the first and last words with dots so that it fits within the container. For example, Sydney - ... - Quito. It should only replace wh ...

Setting a maximum height using flex property of 1 while also enabling overflow

Imagine a scenario where you have a <div with a height specified as a percentage of its parent's height, and this <div contains a contenteditable element as its child. The goal is to make the child fill all available vertical space and scroll wh ...

Leveraging multiple css classes within a single class definition

I need some help with this issue, it seems pretty basic but I can't seem to find the solution...sorry for asking a beginner question. Currently, I have multiple HTML elements that share the same CSS classes: <a class="class1 class2 class3">< ...

Guide: Aligning Content to the Left within a Centered Container

Issue I am facing a challenge in aligning blocks to the left within a centered wrapper that has a dynamic width. Despite trying only HTML and CSS, I have been unsuccessful in achieving this. To see an example of what I am dealing with, you can view this ...

Bug in Chrome (Win) causing middle-mouse-button overflow issue

Take a look at this demo: http://jsfiddle.net/pixelfreak/AN5Wb/ Everything works perfectly until attempting to scroll using the middle mouse button. At that point, you can see beyond the boundaries of the element. This issue does not occur in Firefox or ...

Achieving Vertical Tabs on Larger Screens and Horizontal Tabs on Smaller Screens with Css Bootstrap 4

I'm attempting to create tabs that are vertical on wider screens and horizontal on smaller screens, but I'm unsure of how to accomplish this. I have experimented with adding flex-md-column and flex-lg-column to the nav tabs: <ul class="nav na ...

Ajax function implemented successfully with stylish design

Hi there! :) I'm encountering an issue with my code. I am trying to create a dropdown filter using ajax, php, and json. The first dropdown menu (for selecting the build year of a car) is populated through php. The second dropdown menu allows users to ...

div container overflowing with numerous unordered list elements

I am having an issue with a div and ul in my HTML code. The height of the div is smaller than the combined height of all the li elements within it, so I have set overflow to auto in the CSS for the div. Here is an example of the HTML code: <div class= ...

Unveiling the intricacies of CSS specificity

Struggling with the specificity of this particular CSS rule. Despite researching extensively, I still can't seem to grasp it. Can someone help me determine the specificity of the following: #sidebar h1, p em, p a:hover{ ... } ...

Creating a horizontal line to divide floating list items with CSS

I would like to achieve a design where there is a line between floating items displayed as follows: .......................................... . . . LI LI LI LI LI LI LI LI LI LI LI . . --------------------- ...

What is the best way to apply CSS to a mat-row element only when it is being hovered over?

I have a table with rows where a specific condition triggers a light red background color for each row. On hover, the background changes to light gray for all rows. However, I need the special rows (already colored light red) to have a deeper shade of red ...

The challenge of handling overflow in IE9

I'm encountering an issue specifically in IE9 where a child div is not respecting the overflow:hidden property of its container div. The outlined blue div in the image represents the container with overflow:hidden. The goal is for the images to stay w ...

Issue with removing a row from a table in AngularJS

#AngularJS I'm having an issue with deleting rows from a table. Whenever I click the DELETE button on any row, it always deletes the first one instead of the clicked row. Can someone help me identify where I went wrong? Here's a snippet of the co ...

Having trouble getting my local website to load the CSS stylesheet through Express and Node.js in my browser

https://i.stack.imgur.com/qpsQI.png https://i.stack.imgur.com/l3wAJ.png Here is the app.js screenshot: https://i.stack.imgur.com/l3wAJ.png I have experimented with different combinations of href and express.static(""); addresses. However, I am ...

Tips for creating a custom Menu with content overflow in a single direction

What is the goal of this task? I have developed a custom Menu in my application with 8 options. There is a need to dynamically disable certain menu options based on specific conditions (e.g. disabling the last 4 options). When a user hovers over a disable ...

Focused on enhancing the Woocommerce shop page navigation

I am struggling to change the logo and menu item colors in Woocommerce without success. The rest of my website uses the standard navigation, but I want a different logo and color scheme on all the shop-related pages. This means hiding one logo and menu col ...

disable td's from moving when hovered over

When you add a book, you will see the item added to the table with a cool font and an X icon for removal. However, I noticed that when hovering over the icon to increase its size, it slightly shifts the text in the other cells. Is there a way to prevent th ...

Resizing the window causes divs to be positioned relative to the image (almost functional)

In my coding project, I've encountered a situation where I have 3 "pins" positioned within a div called #outerdiv. This div has a background-image and the pins are supposed to remain in the same spot on the background image no matter how the window is ...

I'm working with Angular 12, Bootstrap 5, and ngPrime, and I'm looking to overlap a p-dialog with another element in my project

Is there a way in Angular 12 with Bootstrap 5 using ngPrime to overlap a p-dialog over any other element without using z-index and CSS, solely relying on PrimeNG? I have tried using z-index with: .my-class{ z-index: 2147483647 !important; } However, ...