11

I'm trying to create new Twitter Bootstrap buttons in my Rails app so I don't have to override the old ones. Using the Bootstrap-Sass gem, I've figured out how to override the default buttons like so (to make the btn-warning class 'Twitter blue'):

$twitterBlue:#4099FF;
$btnWarningBackground: $twitterBlue; // (twitter blue)
$btnWarningBackgroundHighlight:     $twitterBlue;

I'd like to define a btn-twitter class, among others, but I can't seem to figure out how to copy all the Bootstrap styles into new buttons. I've tried things like:

$twitterColor: #4099FF;             // (twitter blue)
$btnTwitterBackground:              lighten($twitterColor, 15%);
$btnTwitterBackgroundHighlight:     $twitterColor;

.btn-twitter {
   background: $twitterBlue;
   // something goes here to inherit the base button styles
}

But that just makes a button with the default style.

I'm still fairly new to SASS and don't know much about the magic that's happening behind the scenes with the LESS -> SASS conversion. Also haven't been able to find anything on the 'net about creating new buttons. If there's something out there, feel free to just point me to it :)

Thanks in advance!

EDIT While not perfectly related to the question, I figured I'd provide a super-handy link to the Bootstrap-Sass variable sheet which has helped me quite a bit with extending Bootstrap in my Rails project: https://github.com/thomas-mcdonald/bootstrap-sass/blob/master/templates/project/_variables.scss.

4

4 回答 4

11

在 Bootstrap 3.1 中,默认按钮如下所示:

.btn-default {
  @include button-variant($btn-default-color, $btn-default-bg, $btn-default-border);
}

您可以在代码中以相同的方式使用它,但请注意,这仅在引导导入之后才有效(与变量覆盖不同,它必须首先出现)。

于 2014-01-31T08:17:16.233 回答
8

你很有可能使用 Sass 的@extend功能来实现这一点。

.btn-success {
  // some crazy unknown bootstrap stuff here
}

// in your file
.btn-twitter {
  @extend .btn-success;
  background-color: $twitterBlue;
  // more stuff
}
于 2013-06-03T15:29:25.610 回答
2

您可以使用button-variant已包含在中的 mixin bootstrap-sasshttps ://github.com/twbs/bootstrap-sass/blob/master/assets/stylesheets/bootstrap/mixins/_buttons.scss#L6

例如:

.btn-secondary {
  @include button-variant(white, #F9622F, #d75124);
}

此外,您还可以找到其他有用的 mixin,例如本文中列出的那些:https ://www.sitepoint.com/5-useful-sass-mixins-bootstrap/

于 2016-11-21T02:47:04.693 回答
0

看看mixin文件_mixins.scss

// Button backgrounds
// ------------------
@mixin buttonBackground($startColor, $endColor, $textColor: #fff, $textShadow: 0 -1px 0 rgba(0,0,0,.25))
...

新的自定义按钮很容易创建:

.mybutton{
    @extend .btn;
    @include buttonBackground($startColor:$bluelight,$endColor:$bluenormal);
}
于 2013-08-21T06:27:21.787 回答