Jas*_*mat 5 css sass twitter-bootstrap reactjs bootstrap-5
我正在使用bootstrap.build 自定义引导程序 5 。到目前为止,我没有问题。

但是,当我切换到反应项目时,事情出错了。按钮内的文本变为黑色。悬停时的大纲按钮也是如此。

我所做的只是自定义.scss文件,例如:
$orange: #e84c22;
$primary: $orange;
@import "~bootstrap/scss/bootstrap.scss";
Run Code Online (Sandbox Code Playgroud)
怎么了?
我的目标是使用确切的#e84c22颜色更改这些按钮以仅使用btn btn-outline-primary和btn btn-primary类显示白色文本。
我试过更改$yiq-contrasted-threshold为,200但它没有改变任何东西。
那么,React 中的 Bootstrap 和 bootstrap.build 中的 Bootstrap 有什么区别?背后发生了什么?
Zim*_*Zim 45
查源码发现bootstrap.build/app使用的是Bootstrap 4,而不是Bootstrap 5。颜色对比逻辑从Bootstrap 4变成了Bootstrap 5,颜色对比阈值也有很大不同。
Bootstrap 4 使用 YIQ 色彩空间...
// The yiq lightness value that determines when the lightness of color changes from "dark" to "light". Acceptable values are between 0 and 255.
$yiq-contrasted-threshold: 150 !default;
Run Code Online (Sandbox Code Playgroud)
Bootstrap 5使用WCAG 2.0算法
// The contrast ratio to reach against white, to determine if color changes from "light" to "dark". Acceptable values for WCAG 2.0 are 3, 4.5 and 7.
// See https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast
$min-contrast-ratio: 4.5 !default;
Run Code Online (Sandbox Code Playgroud)
比较两个版本以及颜色逻辑:
使用 4.x 颜色对比逻辑将 $primary 更改为 #e84c22 - 导致浅文本颜色
使用 5.x 颜色对比逻辑将 $primary 更改为 #e84c22 - 导致深色文本颜色
因此,您的 Bootstrap 5 React SASS 自定义可以按预期工作,并且 bootstrap.build 仍在使用较旧的 4.x。
要在 Bootstrap 5(使用 React)中获得浅色文本颜色,您可以更改变量的默认值$min-contrast-ratio...
例如,将 $min-contrast-ratio 从 4.5 更改为 3 会使文本颜色变浅,形成对比#e84c22:
$min-contrast-ratio: 3;
$orange: #e84c22;
$primary: $orange;
@import "~bootstrap/scss/bootstrap.scss";
Run Code Online (Sandbox Code Playgroud)
https://codeply.com/p/Z1tOoBclnH
正如 Bootstrap 5 SASS 中所解释的“WCAG 2.0 可接受的值为 3、4.5 和 7”。或者,您可以使用我的相关答案中描述的 5.x 解决方案。如果您希望进一步自定义 Bootstrap,mestr.app(注意:我是创建者)已更新为 Bootstrap 5.x。