Can CSS variables replace Bootstrap 4 Sass variables?

I'm attempting to customize the default Primary Sass variable in Bootstrap 4 with a custom color using CSS variables within an Angular application. Here is what I have tried:

styles.scss

:root {
  --primary: #00695c;
}

$myPrimary: var(--primary);

$primary: $myPrimary; // This does not work

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

Unfortunately, this approach results in an error during compilation:

Failed to compile.

./src/styles.scss (./node_modules/@angular-devkit/build-angular/src/angular-cli-files/plugins/raw-css-loader.js!./node_modules/postcss-loader/src??embedded!./node_modules/sass-loader/lib/loader.js??ref--15-3!./src/styles.scss)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):

undefined
                                         ^
      $color: var(--primary) is not a color.
    ╷
181 │ $link-hover-color:                        darken($link-color, 15%) !default;
    │                                           ^^^^^^^^^^^^^^^^^^^^^^^^
    ╵
  node_modules\bootstrap\scss\_variables.scss 181:43  @import
  node_modules\bootstrap\scss\bootstrap.scss 9:9      @import
  stdin 19:9                                          root stylesheet
      in C:\Work\node_modules\bootstrap\scss\_variables.scss (line 181, column 43)

Is there a solution to effectively overwrite bootstrap sass variables using css variables?

While researching, I came across another related question, but it did not resolve my specific issue.

Update

In fact, I've developed an Angular component library for internal use. Within this library, I've integrated Theming using CSS variables, enabling dynamic value changes and theme selection by users.

As part of this setup, I have distinct theme files inside the library. Here's an example of one such file:

theme.scss

@import './library-styles-to-be-used-in-app.scss';

:root {
  --primary: #00695c;
  --secondary: #f4f5f6;
}

Subsequently, I import this theme file into my angular app's styles.scss as shown below:

@import '../dist/library/styles/theme.scss';
@import '../node_modules/bootstrap/scss/bootstrap';

The screenshots illustrate that bootstrap css variables have been successfully overridden. However, when utilizing a bootstrap class like btn btn-primary, the old blue color persists.

https://i.sstatic.net/rJdVS.png

https://i.sstatic.net/nvF4p.png

Answer №1

It is important to note that CSS custom properties and SASS variables are distinct entities, each with its unique characteristics.

CSS custom properties have the ability to be altered dynamically through JavaScript at runtime. On the contrary, SASS variables remain fixed once generated and are embedded as static values in the .css file. For instance, SASS's darken() feature modifies input strings (like hex-values) to produce a darker shade of the original color.

An interesting point to consider is that Bootstrap already incorporates CSS custom properties, which suggests that the issue might lie in the application or understanding of these properties. If you elaborate on your objectives, we may be able to provide more targeted assistance.

At this moment, our response remains unchanged: achieving what you described is not currently feasible.

Answer №2

The current limitation in SCSS files prevents the use of expressions or formulas with this technique.

There is an open issue on GitHub to address this by moving the expressions out of SCSS styles and allowing them to be set using separate variables. You can follow the progress and see the proposed changes for Bootstrap, specifically for enabling CSS variables mode for the alert component, in this pull request: https://github.com/twbs/bootstrap/pull/31753

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

Tips for adjusting child elements to fit within their parent container even after zooming in

Is there a way I can ensure that the child div contents fit inside the parent div, even when zooming in on the screen? How can I prevent the child div from coming out of the parent div? <div> <div title="childDiv" style="width:auto;" ...

Guide to shifting an image to the right within a Bootstrap 4 card header

When working on my website with Bootstrap, I ran into an issue with aligning a text to the left side of the header and an image to the right side. I attempted to use a row container along with justify-content-end, but they did not line up at the same heigh ...

RXJS buffering with intermittent intervals

Situation: I am receiving audio data as an array and need to play it in sequence. The data is coming in continuously, so I am using an observable to handle it. Since the data arrives faster than it can be played, I want to use a buffer to store the data w ...

Trouble with displaying mobile CSS correctly

Take a look at this image and screenshot. Whenever the link is selected, hovered over or clicked on, a blue background appears on this mobile site. I have already included the CSS code below. The CSS code works fine in desktop browsers but not in mobile ...

I am facing an issue with updating the mat-table after pushing values to a

I have a uniqueFormGroup with UniqueFormArray and a special-table that displays the array. When I add new uniqueFormGroup to UniqueFormArray, the special-table doesn't add new row. I was attempting to implement trackBy, but I am unsure of where (and ...

Exploring elementary functionalities within PHP

I'm just starting out, so please excuse any confusion in how I phrase this. Currently, I'm diving into my online computer science course which is focused on functions. The task at hand is to create sentences that output three different variables ...

Sliding Menu with jQuery

My goal is to create a dynamic menu that reveals sub-menu items by sliding them down and changing the background color upon clicking. I am currently using the HTML code displayed below, with the sub1 and sub2 elements initially hidden using display:none; ...

Applying flex-box css for creating a dynamic page header with different types of elements

I am having trouble styling a page header in our application using the flex display property. Specifically, I am struggling to align my class device-name-test to float right on the dashboard header. <div class="diagnosis-header" [class.disabled]="heade ...

When a page is being redirected using requestdispatcher in a filter, the CSS contents do not seem to be applying correctly

Upon evaluation of the condition, if it fails, I am utilizing a request dispatcher to redirect to another page. The redirection is successful, however, the CSS is not being applied to the new page. What could be causing this issue? public class RolePrivil ...

Unable to load the content within the iFrame

While attempting to retrieve the URL of an older website, I encountered an error: Fetch API cannot load http://xyz. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://abc' is therefore not allo ...

How come the Bootstrap 4 row doesn't stretch to the entire available width?

It seems that flexbox should fill all available space in width, but for some reason, the row I inspected is not doing so. Even though it's set to display flex and there doesn't seem to be anything unusual about it, Chrome indicates that there is ...

Creating links within a single image in HTML?

Is there a way to hyperlink different parts of a single image to various webpages using JavaScript coordinates or any other method? ...

What strategies work well for guiding individuals to different destinations based on their user roles, such as administrators and regular users?

In my CMS environment, I am facing a challenge with redirecting users who are not administrators or do not own the document they're trying to edit. The following code snippet is what I have implemented, but it seems that my administrators cannot acces ...

Ways to eliminate models that are not currently connected to the DOM

Within my form, I have implemented logic using ng-if to determine which elements are displayed based on user selections. For instance, if a user selects USA as the country, the state drop down will be shown as it was conditioned upon an ng-if for the count ...

What is the best way to position href text on the top of a rectangular div box?

Whenever I try to position the text above the rectangle, it disables the link. How can I resolve this issue? Additionally, when attempting to change the color of the "San Diego" text, I'm unable to adjust its position. Just a heads up, I am new to HT ...

Attempting to dynamically change the text of a button in JavaScript without any user interaction

I have created a button function that displays a word's definition when clicked. However, I am now attempting to modify it so that the definitions are shown automatically every few seconds using "SetInterval" without requiring a click. I am unsure of ...

Issue with Angular4: Unable to select [selected] attribute on initial load

Here is a code snippet to select a department: <select name="department" class="form-control select" [(ngModel)]="departments" formControlName="departmentControl"> <option *ngFor="let department of departments" [ngValue]="department" [se ...

The method _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵvid"] cannot be executed as a function

While conducting E2E tests for my Angular 6 application using Cypress, I encounter an intermittent error: _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵvid"] Strangely, making a minor code change sometimes resolves the issue. I am puzzled as to why this ...

Exclude striped tr elements within a subtable

I have a main table (Maintable) containing information, with each row having a sub-table for additional details that can be collapsed if needed. I am trying to stripe the rows in the Main table but using `tr:nth-child(even)` is not working as it also affec ...

What could be the issue preventing .find from functioning correctly with this other css selector in JQuery?

I'm facing an issue with the .find method when using a name=value selector. Despite having the correct syntax and knowing that the object I am selecting from contains 7 elements with the desired attribute, the .find is unable to locate any elements. T ...