是否可以创建一个从另一个CSS类(或多个)“继承”的CSS类。
例如,假设我们有:
.something { display:inline }
.else { background:red }
我想做的是:
.composite
{
.something;
.else
}
其中“.composite”类将显示为内联,并具有红色背景
是否可以创建一个从另一个CSS类(或多个)“继承”的CSS类。
例如,假设我们有:
.something { display:inline }
.else { background:red }
我想做的是:
.composite
{
.something;
.else
}
其中“.composite”类将显示为内联,并具有红色背景
当前回答
给定示例的SCSS方式如下:
.something {
display: inline
}
.else {
background: red
}
.composite {
@extend .something;
@extend .else;
}
更多信息,请查看sass基础知识
其他回答
不,你不能这样做
.composite
{
.something;
.else
}
这不是OO意义上的“类”名称。something和.else只是选择器而已。
但是您可以在一个元素上指定两个类
<div class="something else">...</div>
或者你可以研究另一种形式的继承
.foo {
background-color: white;
color: black;
}
.bar {
background-color: inherit;
color: inherit;
font-weight: normal;
}
<div class="foo">
<p class="bar">Hello, world</p>
</div>
其中段落背景颜色和颜色继承自封闭div中的设置,该div为.foo样式。您可能需要检查确切的W3C规范。无论如何,inherit是大多数财产的默认值,但不是所有属性的默认值。
看看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;
}
我在我的反应项目中使用它。
完美的时机:我从这个问题转到了我的电子邮件,找到了一篇关于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
}
CSS并没有真正做到你所要求的。若你们想在写规则的时候考虑到这个复合的想法,你们可能想看看指南针。这是一个样式表框架,看起来类似于前面提到的Less。
它让你做调酒和所有的好生意。