简体   繁体   English

用 CSS 变量覆盖 Bootstrap 4 Sass 变量?

[英]Overwrite Bootstrap 4 Sass variables with CSS variables?

I am trying to overwrite default Primary Sass variable in Bootstrap 4 to a custom color using CSS variables in an Angular application, like this:我正在尝试使用 Angular 应用程序中的 CSS 变量将 Bootstrap 4 中的默认 Primary Sass 变量覆盖为自定义颜色,如下所示:

styles.scss样式.scss

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

$myPrimary: var(--primary);

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

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

I get this error when compiling my application:编译我的应用程序时出现此错误:

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 way to solve this issue and overwrite bootstrap sass variables using css variables?有没有办法解决这个问题并使用css变量覆盖bootstrap sass变量?

There is another related question , But I am unable to solve my issue with it.还有另一个相关问题,但我无法解决我的问题。

Update更新

Actually I've created an Angular component library to be used internally.实际上我已经创建了一个内部使用的 Angular 组件库。 I have implemented Theming in this library using CSS variables, so I can change their values dynamically and allow the user to select a theme for the application.我已经使用 CSS 变量在这个库中实现了主题化,因此我可以动态更改它们的值并允许用户为应用程序选择一个主题。

So inside my library I have different theme files, Below is one of them:因此,在我的库中,我有不同的主题文件,以下是其中之一:

theme.scss主题.scss

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

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

Now, I import this theme file inside my angular app styles.scss file like this:现在,我将这个主题文件导入到我的 angular 应用程序style.scss文件中,如下所示:

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

See the images below bootstrap css variables are already overwritten, but if I use bootstrap class like btn btn-primary it still shows that old blue color.请看下面的图片 bootstrap css 变量已经被覆盖,但是如果我使用像btn btn-primary这样的 bootstrap 类,它仍然显示旧的蓝色。

Bootstrap CSS 变量

我的主题 CSS 变量

This is not possible since CSS custom properties and SASS variables are two seperate things.这是不可能的,因为 CSS 自定义属性和 SASS 变量是两个独立的东西。

CSS custom properties can change during runtime via Javascript. CSS 自定义属性可以在运行时通过 Javascript 更改。
SASS variables on the other hand are static, they will be generated and are then hardcoded values in the .css -File.另一方面,SASS 变量是静态的,它们将被生成,然后是.css文件中的硬编码值。 For example, the darken() functionality from SASS takes the input string (for example a hex-value) and outputs a darkened version of this hex-value.例如,SASS 的darken()功能采用输入字符串(例如十六进制值)并输出此十六进制值的变暗版本。

But bootstrap already uses CSS custom properties, so maybe you are just using them in the wrong way.但是 bootstrap 已经使用了 CSS 自定义属性,所以也许您只是以错误的方式使用它们。 Maybe if you expand what you want to achieve maybe we can help you better.也许如果您扩展您想要实现的目标,也许我们可以更好地帮助您。

For now the only answer we can give you is: This is not possible.目前我们可以给你的唯一答案是:这是不可能的。

Currently expressions (formulas) in SCSS files won't allow to use this technique.当前 SCSS 文件中的表达式(公式)不允许使用此技术。

There is an github issue to move those expressions outside from SCSS styles and allow to set it by separate variables https://github.com/twbs/bootstrap/issues/31538 , eg you can see a PR which shows which changes need to have in Bootstrap and how to enable CSS variables mode for alert component https://github.com/twbs/bootstrap/pull/31753有一个 github 问题可以将这些表达式从 SCSS 样式移出并允许通过单独的变量https://github.com/twbs/bootstrap/issues/31538进行设置,例如,您可以看到一个 PR,其中显示了需要进行哪些更改在 Bootstrap 中以及如何为警报组件启用 CSS 变量模式https://github.com/twbs/bootstrap/pull/31753

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM