css - 创建带边框和不带边框的 Mixin

标签 css sass mixins

我正在尝试创建一个 Mixin,它从两个变量中取一个并根据传递的变量创建一个填充按钮或轮廓按钮。

@include button-style($color: red);

// would result in
background-color: transparent;
color: red;
box-shadow: inset 0 0 0 1px red;

@include button-style($bg: red);

// would result in
background-color: red;
color: white;

有没有办法做到这一点?我在这里疯狂地试图找出实现这一目标的最简单方法。这是我到目前为止所得到的。

@mixin button-style($bg: transparent, $color: white) {
  background-color: $bg;
  color: $color;
  @if $color == 'white' {
    box-shadow: inset 0 0 0 1px $color;
  }
}

感谢任何帮助。提前致谢!

最佳答案

添加一个额外的参数并对其进行检查。

@mixin button-style($bg: transparent, $color: white, $border: true) {
  background-color: $bg;
  color: $color;
  @if $border {
    box-shadow: inset 0 0 0 1px $color;
  }
}

.foo {
  @include button-style;
}

.bar {
  @include button-style($border: false);
}

输出:

.foo {
  background-color: transparent;
  color: white;
  box-shadow: inset 0 0 0 1px white;
}

.bar {
  background-color: transparent;
  color: white;
}

或者,您可以使用空值:

@mixin button-style($bg: transparent, $color: white, $border: inset 0 0 0 1px $color) {
  background-color: $bg;
  color: $color;
  box-shadow: $border;
}

.foo {
  @include button-style;
}

.bar {
  @include button-style($border: null);
}

关于css - 创建带边框和不带边框的 Mixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27842558/

相关文章:

ruby-on-rails - 如何在 factory_girl 工厂中包含模块?

Javascript 工具提示插件 - 克隆和生成标记而不是使用指定的原始标记的原因

html - 使用 flexbox 在跨度和段落之间创建一条水平线

css - Ymacs 中的条形光标

css - SASS 字体混合问题?

c++ - 静态接口(interface) - CRTP?混合?其他?

jquery - 无法更改 css 模态 Bootstrap 。 Bootstrap 中的模态淡入淡出自动添加 padding-left 15px

css - Scss mixin 函数变量作用域

typescript - Ionic 4 + Angular 6 PWA 样式不起作用/损坏

html - SVG 不使用类颜色?