【问题标题】:Bootstrap 5 - Custom theme-colors not updating classesBootstrap 5 - 自定义主题颜色不更新类
【发布时间】:2021-10-15 16:37:01
【问题描述】:

我刚刚使用 Bootstrap 5 开始了一个新项目,我正在尝试使用一些自定义值设置主题颜色。然而,按照我一直这样做的方式这样做会给我带来一些问题。

我创建了三种颜色:$primary、$secondary、$tertiary。但是,如果我添加任何类(例如 bg-tertiary),则不会发生任何变化,就好像它不存在一样。 bg-primary 只是使用 Bootstrap 定义的默认颜色。

我的代码如下:

@import "bootstrap/_functions";
@import "bootstrap/_variables";

$primary: #ec008c;
$secondary: #1ab7ea;
$tertiary: #3fb247;

$theme-colors: (
    "primary": $primary,
    "secondary": $secondary,
    "tertiary": $tertiary,
    "light": $light,
    "dark": $dark,
);

@import "bootstrap/bootstrap";

如果我将默认值(例如“dark”)更改为使用 $tertiary,则 scss 文件中使用 $dark 的任何代码都会更改为使用来自 $tertiary 的值。如下:

$theme-colors(
    "dark": $tertiary
);

#pageFooter {
   background: $dark; //This becomes #3fb247 the value from $tertiary
}

我做错了什么?我不明白为什么 scss 文件中的变量会受到 $theme-colors 更改的影响,但类不会。

编辑:

使用 chrome 检查器,我可以看到 .bg-primary 使用 css 变量 --bs-primary-rgb。查看可用变量--bs-primary 已更改为我设置的颜色,但不是--bs-primary-rgb。

如何更改此变量。应该自动完成吗?

随着进一步研究,这些 rgb 变量似乎已在 Bootstrap 5.1 中引入。我找不到太多关于如何让变量更新为我的设定值的信息,可能是因为它太新了。所以我选择恢复到 5.0.2,现在一切都按照我的预期工作。

【问题讨论】:

    标签: twitter-bootstrap sass bootstrap-5


    【解决方案1】:

    引导 5.1.0

    最近对 text- 和 bg- 类创建方式的更改需要在 5.1.0 中进行额外的 SASS 映射合并

    @import "functions";
    @import "variables";
    @import "mixins";
    
    $tertiary: #3fb247;
    
    $theme-colors:map-merge($theme-colors, (
      "tertiary": $tertiary
    ));
    
    $theme-colors: map-merge($theme-colors, $custom-theme-colors);
    $theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");
    $utilities-colors: map-merge($utilities-colors, $theme-colors-rgb);
    $utilities-text-colors: map-loop($utilities-colors, rgba-css-var, "$key", "text");
    $utilities-bg-colors: map-loop($utilities-colors, rgba-css-var, "$key", "bg");
    
    @import "bootstrap";
    

    引导 5.0.2

    如果您希望它生成像bg-tertiary、@987654327 这样的类,则需要添加一个“第三级”变量到使用map-merge 的主题颜色映射中@等……

    @import "functions";
    @import "variables";
    @import "mixins";
    
    $tertiary: #3fb247;
    
    $theme-colors:map-merge($theme-colors, (
      "tertiary": $tertiary
    ));
          
    @import "bootstrap";
    

    Demo

    【讨论】:

    【解决方案2】:

    如果要覆盖引导程序的变量,则不需要使用以下代码。

    $theme-colors: (
        "primary": $primary,
        "secondary": $secondary,
        "tertiary": $tertiary,
        "light": $light,
        "dark": $dark,
    );
    
    

    这就够了。 无论在何处使用这些颜色都会被覆盖。

    $primary: #ec008c;
    $secondary: #1ab7ea;
    $tertiary: #3fb247;
    
    

    如果您想覆盖$dark,请执行以下操作:

    $dark: $tertiary;
    
    

    【讨论】:

    • 不幸的是,这仍然无法正常工作。它将覆盖 scss 文件中的任何内容。但是,如果我将 .bg-primary 添加到一个元素,它仍然是默认的引导蓝色,而不是我设置的值。查看检查器显示它正在从 _utilities.scss 获取值,现在是否应该从我的自定义 scss 文件中获取值?
    • @Edward144 使用 [@]import "bootstrap/bootstrap";在顶部。
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2015-10-04
    • 2017-11-29
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多