【问题标题】:SASS & LESS - extending a separated ClassSASS & LESS - 扩展一个分离的类
【发布时间】:2017-04-29 21:27:51
【问题描述】:

只是想知道是否存在任何方法来扩展分离类的前两个,例如示例中,或者是否存在任何其他选项,例如创建特定类 .background{background:red} 并将其用作扩展而不是单独的类(但我不想在 CSS 中输出类 .background)。

示例

SASS:

.foo {
  background:red
}
.foo {
  color:red
}
.bar {
  @extend .foo;
}
.foo {
  font-size: 16px
}

少:

.foo {
  background:red
}
.foo {
  color:red
}
.bar {
  &:extend(.foo);
}
.foo {
  font-size: 16px
}

CSS 中的输出将是:

.foo, .bar {
  background: red;
}

.foo, .bar {
  color: red;
}

.foo, .bar {
  font-size: 16px;
}

但我想变成这样:

.foo, .bar {
  background: red;
}

.foo, .bar {
  color: red;
}

// No .bar class here

.foo {
  font-size: 16px;
}

我应该遵循什么方法来实现这一点?

【问题讨论】:

    标签: css sass less extend


    【解决方案1】:

    你的继承权倒退了。 bar 不扩展 foofoo 扩展 bar

    LESS:
    .bar {
      background-color: red;
    }
    
    .bar {
      color: red;
    }
    
    .foo {
      &:extend(.bar);
      font-size: 16px;
    }
    

    生产

    CSS:
    .bar,
    .foo {
      background-color: red;
    }
    .bar,
    .foo {
      color: red;
    }
    .foo {
      font-size: 16px;
    }
    

    【讨论】:

    • 你能不能把你的LESS代码中的两个.bar声明合并到一个选择器中?
    • @TylerH,是的,但我尽可能地遵循 OP 的示例。
    • 您好 zzzzBov,感谢您的回复!我可能错误地提出了这个问题,但是我想要一些不同的东西。在您的 LESS 代码中,您将 font-size 放在类 .foo 中,并且您的 expand 规则仅扩展类 .bar(我知道这一点)。但我想知道我们是否可以在 expand 不加载所有分隔的.bar 的情况下,只加载 expand 调用它之前的两个,而另一个不加载底部的展开...希望你明白我真正想要的是什么:)
    • @JoziBashaj,我第一次理解你的问题,你不能按照你追求的方式去做你追求的事情,这就是为什么我表示你已经得到了你的向后继承。你塑造你的风格的方式可能是倒退的。考虑只使用 mixins 来共享 .foo.bar 之间的共同样式。
    猜你喜欢
    • 2013-09-19
    • 2017-04-04
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多