[英]Changing Bootstrap theme-colors
Just for the sake of easy reference, I'm creating new theme-colors as per below so I could use bg-red
instead of bg-danger
or text-purple
and so on.只是为了方便参考,我正在创建新的主题颜色,如下所示,这样我就可以使用bg-red
而不是bg-danger
或text-purple
等等。
$primary : #24305e;
$blue : #375abb;
$indigo : #796eff;
$purple : #6f42c1;
$pink : #e83e8c;
$red : #db4446;
$orange : #e3240C;
$yellow : #ffc107;
$green : #21c87a;
$teal : #2e9cca;
$cyan : #17a2b8;
$light : #f6f9fc;
$theme-colors : (
"blue" : $blue,
"indigo" : $indigo,
"purple" : $purple,
"pink" : $pink,
"red" : $red,
"orange" : $orange,
"yellow" : $yellow,
"green" : $green,
"teal" : $teal,
"cyan" : $cyan
);
@import "node_modules/bootstrap/scss/bootstrap";
However, I'd like to know if this is the right way of doing it as I do notice that I'm having a duplicate :root
variable colors.但是,我想知道这是否是正确的做法,因为我注意到我有一个重复的:root
变量 colors。 Please refer to the attached image.请参考附图。
Am I missing something here?我在这里错过了什么吗?
That's because CSS variables are created from $colors: ()
map, not from $theme-colors:()
map.这是因为 CSS 变量是从$colors: ()
:() map 创建的,而不是从$theme-colors:()
map 创建的。
You are overriding already created CSS variables by using the same name for colors.您通过使用 colors 的相同名称来覆盖已创建的 CSS 变量。 If for, example do in如果为,例如做
$theme-colors:( 'vermilion':$red, )
in root:
you would see both --vermilion:
and --red:
在root:
您会同时看到--vermilion:
和--red:
UPDATE: it is same as if in normal.css file you do:更新:就像在 normal.css 文件中一样:
:root {
--red: #222222;
--red: #333333;
}
--red: #222222;
would be crossed and browser would use second --red: #333333;
将被交叉,浏览器将使用第二个--red: #333333;
Bootstrap defines css variables for both the $colors
and the $theme-colors
maps. Bootstrap 为$colors
和$theme-colors
映射定义了 css 变量。 Since the default $colors
map includes many of the same colors that you are defining as $theme-colors
as well, such as blue
, indigo
, purple
etc. those will have the related CSS variables output twice. Since the default $colors
map includes many of the same colors that you are defining as $theme-colors
as well, such as blue
, indigo
, purple
etc. those will have the related CSS variables output twice.
The default $colors
map is defined as (v4.5.2):默认$colors
map 定义为 (v4.5.2):
(
"blue": $blue,
"indigo": $indigo,
"purple": $purple,
"pink": $pink,
"red": $red,
"orange": $orange,
"yellow": $yellow,
"green": $green,
"teal": $teal,
"cyan": $cyan,
"white": $white,
"gray": $gray-600,
"gray-dark": $gray-800
)
It should not cause any issues as long as you are using the same color values for the same name in both $colors
and the $theme-colors
, or in your case as long as you are setting your $theme-colors
map matching any of the keys in the default $colors
map to the same variables.只要您在$colors
和$theme-colors
中使用相同名称的相同颜色值,或者在您的情况下设置$theme-colors
map 匹配任何默认$colors
map 中的键到相同的变量。 So in your case everything is normal and there's no need to worry at all.*因此,在您的情况下,一切正常,完全无需担心。*
If you would like to set for example $blue
to a different value than your "blue"
item in $theme-colors
, than you'd have an issue.例如,如果您想将$blue
设置为与$theme-colors
中的"blue"
项目不同的值,那么您就会遇到问题。 In that case you'd need to use a different name in your $theme-colors
map, eg "blue2"
or similar.在这种情况下,您需要在$theme-colors
map 中使用不同的名称,例如"blue2"
或类似名称。
You may find the double presence of these CSS variables annoying, but I guess it is like that so if you by accident would override one of your own colors, because of the interactions between您可能会发现这些 CSS 变量的双重存在令人讨厌,但我想就是这样,所以如果您不小心会覆盖您自己的 colors 之一,因为它们之间的相互作用
$blue
),颜色名称变量(例如$blue
),colors()
map (which contains a "blue": $blue
declaration and results in a --blue
css variable added by default) and默认colors()
map (其中包含一个"blue": $blue
声明并导致默认添加--blue
8CBA22E28EB17B5F5C6AE2A266AZ 变量)和$theme-colors
map (which may be customised to also contain an item with the key "blue":...
and would also create a --blue
css variable). $theme-colors
map (可以定制为还包含一个带有"blue":...
并且还会创建一个--blue
css 变量)。Not filtering out these potential duplicate declarations could give you a starting point to debug the issue.不过滤掉这些潜在的重复声明可以为您提供调试问题的起点。 Otherwise it could be much harder to figure out what is going on just by looking at the styles in your browser.否则,仅通过在浏览器中查看 styles 就很难弄清楚发生了什么。
*: Note that each theme-color adds some size to your final CSS, so it's best to only include those, which are needed for sure. *:请注意,每种主题颜色都会为您的最终 CSS 增加一些尺寸,因此最好只包括那些肯定需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.