是否可以将一个 CSS 类的样式包含到另一个类中?我的意思是 SASS @extends 几乎做了类似的事情,但它也为扩展类设置了样式,这不是必需的。参见示例:
<style>
.myclass1{
background:red;
}
.myclass2{
color:blue;
@extend .myclass1;
}
</style>
<div>
<p class="myclass2">Hello i am class 2, my text is blue and background is red</p>
</div>
<div>
<p class="myclass1">Hello i am class 1, my text should not blue and my background is red</p>
</div>
发布的示例灵感来自 CSS-tricks Web 的文章,但这里的一切都非常困惑,它没有按预期工作。 myclass2 应该根据文章给出myclass1。但是,它给出了奇怪的输出。我正朝着正确的方向前进吗?还是文章有误?
更新: 问题只是关于 SAAS 的 @extend 背后的实际概念,以及将其他 CSS 类包含到另一个,有什么区别?
最佳答案
没有预处理器的 CSS(SASS、LESS 等):
.myclass1{
background:red;
}
.myclass2{
color:blue;
}
<div>
<p class="myclass1 myclass2">Hello, I am of class 2 (my text is blue) and of class 1 (my background is red)</p>
</div>
<div>
<p class="myclass1">Hello, I am only of class 1 (my background is red)</p>
</div>
您可以向同一个元素添加多个类。
关于html - 将一个 CSS 类的样式包含到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227416/