如何覆盖Bootstrap 5.0的信息颜色,使其类似于Bootstrap 3的信息颜色?

huangapple go评论60阅读模式
英文:

How do I override bootstrap 5.0 info color and make it like bootstrap 3's info color?

问题

在Bootstrap 3中,info颜色是#d9edf7。当使用bg-infoalert-info时,背景颜色始终是#d9edf7。我尝试覆盖Bootstrap 5.0以获得相同的info颜色效果。

我有一个自定义的SCSS文件来覆盖Bootstrap:

如果我将info或cyan变量设置为#d9edf7,那么我的.bg-info元素看起来正确,但我的.alert-info元素会变亮60%。

$info: #d9edf7;
@import "../scss/bootstrap.scss";

如果我将info或cyan变量设置为#40a4d7,那么我的.alert-info元素看起来正确,但我的.bg-info元素比#d9edf7要暗40%。

$info: #40a4d7;
@import "../scss/bootstrap.scss";

如何覆盖Bootstrap以获得所需的效果?基本上,使用info颜色的任何元素都应该具有背景颜色#d9edf7

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="alert alert-info" role="alert">
  A simple info alert—check it out!
</div>

<div class="bg-info" style="width:100%; height:60px;">
  This should have the same background color because they're both using the 'info' color
</div>
英文:

In bootstrap 3, the info color was #d9edf7. When using bg-info or alert-info, the background color was always #d9edf7. I'm trying to override bootstrap 5.0 to have the same effect on the info color.

I have a cutom scss file to override bootstrap:

If I set the info or cyan variable to #d9edf7, then my .bg-info elements look correct but my .alert-info elements are getting lightened by 60%.

$info: #d9edf7;
@import &quot;../scss/bootstrap.scss&quot;;

If I set the info or cyan variable to #40a4d7 then my .alert-info elements look correct but my .bg-info elements are 40% darker than #d9edf7.

$info: #40a4d7;
@import &quot;../scss/bootstrap.scss&quot;;

How can I override bootstrap to get the desired effect? Basically, anything using the info color should have a background color of #d9edf7.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;div class=&quot;alert alert-info&quot; role=&quot;alert&quot;&gt;
  A simple info alert—check it out!
&lt;/div&gt;

&lt;div class=&quot;bg-info&quot; style=&quot;width:100%; height:60px;&quot;&gt;
This should have the same background color because they&#39;re both using the &#39;info&#39; color
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

Bootstrap 5.0.2

由于在5.0版本中使用了颜色“scaling”,您需要通过将适当的颜色传递到alert-variant混合器中来重新生成alert-info类...

@import "functions";
@import "mixins";

$info: #d9edf7;

@import "variables";
@import "bootstrap";

.alert-info {
    @include alert-variant($info, $info, shift-color($info, $alert-color-scale));
}

在这个示例中,我仍然使用了颜色缩放来设置(前景)颜色,但您可以使用$body-color代替...

@include alert-variant($info, $info, $body-color);

代码示例


Bootstrap 5.3.0

正如您在文档中所读到的...

> "Sass mixin is deprecated. Alert variants now have their CSS variables
> overridden in a Sass
> loop
"

因此,警报文本颜色、背景颜色、链接等都有不同的颜色变量... 在您的情况下,您将希望覆盖$info-bg-subtle以更改背景颜色:

$info: #d9edf7;
$info-bg-subtle: #d9edf7;

@import "bootstrap";

SASS演示

英文:

Bootstrap 5.0.2

Because of the color "scaling" used in 5.0 you'd need to regenerated the alert-info class by passing the appropriate colors into the alert-variant mixin...

@import &quot;functions&quot;;
@import &quot;mixins&quot;;

$info: #d9edf7;

@import &quot;variables&quot;;
@import &quot;bootstrap&quot;;

.alert-info {
    @include alert-variant($info, $info, shift-color($info, $alert-color-scale));
}

In this example I still used the scaling for the (foreground) color, but you could just use $body-color instead...

@include alert-variant($info, $info, $body-color);

https://codeply.com/p/k0MPD65ezI


Bootstrap 5.3.0

As you will read in the docs...

> "Sass mixin is deprecated. Alert variants now have their CSS variables
> overridden in a Sass
> loop
"

So there are different color vars for the alert text color, bg color, links, etc... In your case, you'd want to override $info-bg-subtle for the background color:

$info: #d9edf7;
$info-bg-subtle: #d9edf7;

@import &quot;bootstrap&quot;;

SASS demo

答案2

得分: 0

经过彻底调查,似乎导致您在 Bootstrap 5.0 中出现问题的 SASS 变量是 $alert-bg-scale。此变量在 _variables.scss 文件中声明,初始值为 -80%

$alert-bg-scale: 0%;
$info: #d9edf7;

@import &quot;bootstrap.scss&quot;;

不幸的是,要获得您期望的行为似乎不会像您希望的那么简单。如果您修改此值,将会使所有警报的背景颜色变暗。看起来他们在后续版本中解决了这个问题,详见 Carol Skelly 的回答

我可以想到的一些解决方法如下:

在您当前使用 .bg-info 的地方使用 .alert-info

此类仅在背景颜色之外应用边框和文本颜色。

.bg-info 类创建覆盖

如果您绝对不同意前面的建议设置边框和文本颜色,这个方法也可以。但是,我建议不要这样做,因为这会修改 Bootstrap 的预期行为。或者,您可以创建自己的自定义类来实现此效果。

$info: #d9edf7;

@import &quot;bootstrap.scss&quot;;

/*
  通过在 Bootstrap 导入之后调用此类您可以访问所有的
  SASS 函数特别是 `shift-color()`
*/
.bg-info {
    background-color: shift-color($info, $alert-bg-scale) !important;
}
英文:

After thorough investigation, it appears the SASS variable causing your issue for Bootstrap 5.0 is $alert-bg-scale. This variable is declared in the _variables.scss file with an initial value of -80%.

$alert-bg-scale: 0%;
$info: #d9edf7;

@import &quot;bootstrap.scss&quot;;

Unfortunately, it looks like getting the behavior you desire won't be as simple as you had hoped. If you modify this value, it will make the background color for ALL alerts darker. It appears that they did solve this problem in later versions, per Carol Skelly's answer.

A couple of workarounds I can think of off the top of my head:

Use the .alert-info class wherever you currently are using .bg-info

This class only applies a border and text color in addition to the background-color.

Create an override for the .bg-info class

If you're absolutely against the idea of having the border and text color set by the previous suggestion, this would work. However, I would recommend against this as it would be modifying the expected behavior of Bootstrap. Alternatively, you could create your own custom class that does this.

$info: #d9edf7;

@import &quot;bootstrap.scss&quot;;

/*
  By calling this after the Bootstrap import, you get access to all the
  SASS functions, namely `shift-color()`
*/
.bg-info {
    background-color: shift-color($info, $alert-bg-scale) !important;
}

huangapple
  • 本文由 发表于 2023年6月1日 23:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定