简体   繁体   English

如何使用SASS覆盖Bootstraps“ btn-primary”活动背景色?

[英]How to override Bootstraps “btn-primary” active background-color using SASS?

I am using bootstrap 4 with SCSS and have troubles to find the variable which is responsible for the background of an "btn-primary" element which is active. 我正在将Bootstrap 4与SCSS一起使用,并且很难找到负责“ btn-primary”元素处于活动状态的背景的变量。

Of course I could just override the css file like that after the compiling process. 当然,在编译过程之后,我可以像这样覆盖css文件。

.btn-primary:not(:disabled):not(.disabled):active{
  background-color:red;
}

But this is a bit hacky. 但这有点hacky。 I would like to undestand how bootstrap is generating this particular color? 我想了解引导程序是如何生成这种特定颜色的? I am pretty sure I am missing something. 我很确定我缺少什么。

Please see this jsfiddle and click on the test button to see what I mean. 请参阅此jsfiddle ,然后单击“测试”按钮以了解我的意思。

Solved: The color is stored within $primary (Thank you ReSedano) 解决:颜色存储在$ primary中(谢谢ReSedano)

Just do a 只是做一个

$primary: #ff00ff;

before you load bootstrap. 在加载引导程序之前。

this is a simple error . 这是一个简单的错误。 bootstrasp.css after style.css if yes can you move bootstrap.css before style.css 如果是,您可以将bootstrap.css移到style.css之后

like 喜欢

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="assets/bootstrap/dist/css/bootstrap.min.css">
<!-- custom CSS -->
<link rel="stylesheet" href="css/style.css">

use css in style.css 在style.css中使用CSS

.btn-primary:not(:disabled):not(.disabled) {
background: red;
}

Here you can understand How to override boostrap4 using scss : How to extend/modify (customize) Bootstrap 4 with SASS . 在这里您可以了解如何使用scss覆盖boostrap4如何 使用SASS 扩展/修改(自定义)Bootstrap 4

In your case you have to override the map $theme-colors : 在您的情况下,您必须覆盖地图$theme-colors

$theme-colors: (
  "primary": #ff0000 // <= put here your color
);

To understand "how bootstrap is generating this particular color?" 要了解“引导程序如何生成这种特定颜色?” in _buttons.scss file you can find the function: _buttons.scss文件中,您可以找到以下功能:

@each $color, $value in $theme-colors {
  .btn-#{$color} {
    @include button-variant($value, $value);
  }
}

So to change colors, Bootstrap loop the map call $theme-colors that you find in _variable.scss file. 因此,要更改颜色,Bootstrap将循环调用_variable.scss文件中找到的$theme-colors映射。

$theme-colors: () !default;

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

If you go to https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css which is a cdn for bootstrap, you will see minified version of bootstrap library. 如果您访问https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css(它是引导程序的CDN),则会看到引导程序库的缩小版本。 Everytime you use it as a cdn or as downloaded file, you are using css rules for declared classes from this source. 每次将其用作CDN或下载文件时,都在使用CSS规则从该源中声明类。 Fill free to control + f and search for .btn-primary:not(:disabled):not(.disabled):active to see the rules for this class. 随意填充以控制+ f并搜索.btn-primary:not(:disabled):not(.disabled):active以查看此类的规则。

Also, you are right. 而且,你是对的。 The way you did it is a bit hacky. 您的操作方式有些古怪。 What you should do is put new class on an element on which you want to overrirde bootstrap rules and then in css or scss use it like this 您应该做的是将新类放在要覆盖引导规则的元素上,然后在css或scss中像这样使用它

.btn-primary.mycustombuttonclass {
    background: white; //for example
}

Theme colors is defined in _variables.scss 主题颜色在_variables.scss中定义

Buttons is generated in _buttons.scss and uses button-variant() mixin 按钮在_buttons.scss中生成,并使用button-variant()mixin

// _variables.scss
$theme-colors: map-merge(
  (
    "primary":    $primary,
    "secondary":  $secondary,
    "success":    $success,
    "info":       $info,
    "warning":    $warning,
    "danger":     $danger,
    "light":      $light,
    "dark":       $dark
  ),
  $theme-colors
);

// _buttons.scss
@each $color, $value in $theme-colors {
  .btn-#{$color} {
    @include button-variant($value, $value);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM