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

例如,假设我们有:

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

我想做的是:

.composite 
{
   .something;
   .else
}

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


当前回答

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

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

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

其他回答

这在CSS中是不可能的。

CSS中唯一支持的是比另一条规则更具体:

span { display:inline }
span.myclass { background: red }

类为“myclass”的span将同时具有这两个财产。

另一种方法是指定两个类:

<div class="something else">...</div>

“else”的样式将覆盖(或添加)“something”的样式

一个元素可以包含多个类:

.classOne { font-weight: bold; }
.classTwo { font-famiy:  verdana; }

<div class="classOne classTwo">
  <p>I'm bold and verdana.</p>
</div>

不幸的是,这几乎和你会得到的一样接近。我希望有一天能看到这个特性以及类别名。

看看CSS合成:https://bambielli.com/til/2017-08-11-css-modules-composes/

根据他们的说法:

.serif-font {
    font-family: Georgia, serif;
}

.display {
    composes: serif-font;
    font-size: 30px;
    line-height: 35px;
}

我在我的反应项目中使用它。

不要忘记:

div.something.else {

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

}

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

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