css - Bootstrap v5.1.3 : merge $colors with $theme-colors map

标签 css twitter-bootstrap sass bootstrap-5

我正在尝试添加一些颜色变量,例如“蓝色”“靛蓝” ...到$ theme-colors 映射它可以工作,但编译后它在 :root 选择器中显示重复,如下图所示。

结果如下:

Image

这是我的_variables.scss 文件:

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

$white: #fff;
$gray-100: #f8f9fa;
$gray-200: #e9ecef;
$gray-300: #dee2e6;
$gray-400: #ced4da;
$gray-500: #adb5bd;
$gray-600: #6c757d;
$gray-700: #495057;
$gray-800: #343a40;
$gray-900: #212529;
$black: #000;

$blue: #0d6efd;
$indigo: #6610f2;
$purple: #6f42c1;
$pink: #d63384;
$red: #dc3545;
$orange: #fd7e14;
$yellow: #ffc107;
$green: #198754;
$teal: #20c997;
$cyan: #0dcaf0;

$primary: #c55e0a;
$secondary: $gray-600;
$success: $green;
$info: $cyan;
$warning: $yellow;
$danger: $red;
$light: $gray-100;
$dark: $gray-900;

$theme-colors: (
    "primary": $primary,
    "secondary": $secondary,
    "success": $success,
    "info": $info,
    "warning": $warning,
    "danger": $danger,
    "light": $light,
    "dark": $dark
);

$custom-theme-colors: (
    "blue": $blue,
    "indigo": $blue,
    "purple": $blue,
    "pink": $blue,
    "yellow": $blue,
    "teal": $teal
);

$theme-colors: map-merge($theme-colors, $custom-theme-colors);

// Import required Bootstrap stylesheets
@import "../../../node_modules/bootstrap/scss/variables";
@import "../../../node_modules/bootstrap/scss/mixins";
@import "../../../node_modules/bootstrap/scss/utilities";

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

我该如何解决这个问题?

最佳答案

尽你所能read in the Bootstrap docs ,自定义内容应保存在单独的文件中。因此,您不应修改 _variables.scss,而应使用自定义颜色创建单独的文件。

自定义文件部分将 look like this

例如:custom.scss

@import "bootstrap/functions";
@import "bootstrap/variables";

$custom-colors: (
    "blue": $blue,
    "indigo": $indigo,
    "purple": $purple,
    "pink": $pink,
    "yellow": $yellow,
    "teal": $teal
);

$theme-colors: map-merge($theme-colors, $custom-colors);
      
@import "bootstrap";

此外,预计 :root 变量会出现两次,因为您将它们与主题颜色映射合并。看看 :root 是如何在 _root.scss 中构建的...

首先迭代 $colors 映射..然后迭代 $theme-colors 映射,这样它就会变得完美,因为你看到 --bs-blue、--bs-indigo 等...两次:根...

 @each $color, $value in $colors {
    --#{$variable-prefix}#{$color}: #{$value};
  }

  ....

  @each $color, $value in $theme-colors {
    --#{$variable-prefix}#{$color}: #{$value};
  }

没有什么需要修复的,它按预期工作。如果您可以描述您想要实现的目标而不是描述症状,也许会有不同的答案或更好的解决方案。

关于css - Bootstrap v5.1.3 : merge $colors with $theme-colors map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71374485/

相关文章:

html - sass:当另一个div悬停时影响另一个元素

jquery - css jquery 不起作用

javascript - 第二个表的表头与 queryAllSelector 不正确

html - 显示用于网页导航的平铺 Sprite 图像

javascript - Bootstrap 4 - 位置碰撞 : sticky; and positon: fixed;

css - 如何居中对齐两个元素?

reactjs - 在事件状态下设置 React Bootstrap 组件的样式

javascript - 获取网页标题和段落

javascript - 无法使用 React 和 Bootstrap 创建自定义样式

jquery - 响应式导航选项卡,即可折叠的导航选项卡