是否可以创建一个从另一个CSS类(或多个)“继承”的CSS类。

例如,假设我们有:

.something { display:inline }
.else      { background:red }

我想做的是:

.composite 
{
   .something;
   .else
}

其中“.composite”类将显示为内联,并具有红色背景


当前回答

不要忘记:

div.something.else {

    // will only style a div with both, not just one or the other

}

其他回答

对于那些对上述(优秀)文章不满意的人,您可以使用编程技能创建一个变量(PHP或任何一个),并让它存储多个类名。

这是我能想出的最好的办法。

<style>
.red { color: red; }
.bold { font-weight: bold; }
</style>

<? define('DANGERTEXT','red bold'); ?>

然后将全局变量应用于所需的元素,而不是类名本身

<span class="<?=DANGERTEXT?>"> Le Champion est Ici </span>

我认为这是一个更好的解决方案:

[class*=“button-“] {
  /* base button properties */
}
.button-primary { ... }
.button-plain { ... }

完美的时机:我从这个问题转到了我的电子邮件,找到了一篇关于Less的文章,这是一个Ruby库,除其他外,它还做了以下工作:

由于super看起来就像页脚,但字体不同,所以我将使用Less的类包含技术(他们称之为mixin)来告诉它也包含这些声明:

#super {
  #footer;
  font-family: cursive;
}

不要忘记:

div.something.else {

    // will only style a div with both, not just one or the other

}

给定示例的SCSS方式如下:

.something {
  display: inline
}
.else {
  background: red
}

.composite {
  @extend .something;
  @extend .else;
}

更多信息,请查看sass基础知识