css - LESS mixin 中的条件参数

标签 css less

问题

我创建了以下混入:

.type(@style;@mb) {
    & when (@style = hero) {
        margin-bottom: @mb;
        font-size: 2.625rem;
        line-height: 1.238095238;
    }
}

现在这主要是我想要的。我遇到的问题是有时我想声明一个 @mb 值,但很多时候我不会。在这些情况下,我将希望回退到每个 @style 参数的预定值。

例如:

  • 对于hero@mb默认为margin-bottom: 1.25rem;
  • 对于页面@mb默认为margin-bottom: 1.125rem;
  • 等等

期望的结果

期望的结果如下:

.sample-class-01 { .type(hero); }
.sample-class-02 { .type(page,0); }

我会得到以下输出:

.sample-class-01 { 
    margin-bottom: 1.25rem;
    font-size: 2.625rem;
    line-height: 1.238095238;
}
.sample-class-02 {
    margin-bottom: 0;
    font-size: 2rem;
    line-height: 1.3125;
}

我如何创建这个 mixin?

最佳答案

只需为每个样式集进行 mixin 特化/重载:

.type(hero, @mb: 1.25rem) {
    margin-bottom: @mb;
    font-size: 2.625rem;
    line-height: 1.238095238;
}

.type(not-a-hero, @mb: 2.22rem) {
    margin-bottom: @mb;
    font-size: 3.333rem;
    line-height: 4.444444444;
}

// etc.

引用:

关于css - LESS mixin 中的条件参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42142611/

相关文章:

html - 如何使用 CSS 垂直居中 2 个 anchor 标记?

html - 元素出国有自己的背景

jquery - 使用 jquery .css 均衡两列的高度

css - Visual Studio 2015 中的 LESS

css - Bootstrap 主题中的 Alpha channel 和不透明度

javascript - 使用带有 jquery 的 html 创建侧边菜单

css - 如何在浏览器窗口中居中和调整大小/纵横比未知的 div/img

css - 在 CSS 中混合简单的颜色

vue.js - 如何在nuxt js中使用.less文件

less - Gulp 可以更改 LESS 变量吗?