css - SASS & LESS - 扩展一个单独的类

标签 css sass less extend

只是想知道是否存在像示例中那样仅扩展前两个分离类的方法,或者是否存在任何其他选项,例如创建特定类 .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;
}

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

最佳答案

您的继承权是倒过来的。 bar 不扩展 foofoo 扩展 bar:

减少:
.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;
}

关于css - SASS & LESS - 扩展一个单独的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41144483/

相关文章:

css - :active pseudo class? 鼠标松开后如何改变按钮的css样式

CSS - 边框底部长度固定为 60%

compass-sass - 多个目录中的 compass 源

css - mixin 和 extend 之间的差异较小

html - CSS 列表 : Replacing Bullet with Image and Adding Hover

html - 将页脚上的文本对齐在同一行

css - Autoprefixer 与 LESS/Sass 混合?

html - 编译sass文件 "Error: Expected spaces, was tabs."

ruby - 从命令行编译 LESS 文件时,recess 的错误输出到哪里去了?

javascript - 如何利用插件使用自定义函数扩展 Less 编译器