Bootstrap v5.1.3:将 $colors 与 $theme-colors 映射合并
我正在尝试添加一些颜色变量,例如“蓝色”,“靛蓝” ...到$theme-colors
映射它可以工作,但编译后它在 :root
选择器中显示重复,如下图所示。
结果如下:
这是我的_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";
如何解决此问题?
I'm trying to add some color variables such as "blue", "indigo" ... to $theme-colors
map it's work but after compiling it shows duplicated in :root
selector like the image below.
Here is the result:
this is my _variables.scss file:
// 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";
How I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您可以阅读 Bootstrap 文档,自定义内容应保存在单独的文件中。因此,您不应修改
_variables.scss
,而应使用自定义颜色创建单独的文件。自定义文件部分将看起来像这样
例如:custom.scss
另外,预计 :root 变量由于您将它们与主题颜色贴图合并,因此会出现两次。看看 :root 是如何在
_root.scss
中构建的...首先迭代 $colors 映射..然后迭代 $theme-colors 映射,这样它就会变得完美,因为你'在 :root 中看到 --bs-blue、--bs-indigo 等两次...
没有什么需要修复的,它按预期工作。如果您可以描述您想要实现的目标而不是描述症状,也许会有不同的答案或更好的解决方案。
As you can read in the Bootstrap docs, customizations should be kept in a separate file. Therefore, you shouldn't modify
_variables.scss
but instead create seperate file with your custom colors.The custom file portion would look like this
ex: custom.scss
Also, it's expected that the :root variables would appear twice since you're merging them with the theme-colors map. Take a look at how the :root gets built in
_root.scss
...First the $colors map is iterated.. then the $theme-colors map is iterated so it would make perfect since you're seeing --bs-blue, --bs-indigo, etc... twice in the :root...
There is nothing to fix, it's working as expected. Maybe if you can describe what you're trying to achieve instead of describing a symptom there would be a different answer or better solution.