html - 将一个 CSS 类的样式包含到另一个

标签 html css extends saas

是否可以将一个 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/

相关文章:

html - 与导航下拉菜单相关的问题

jquery - 在一行 float 元素之间添加一个元素

java - 难以理解 <? Java 中扩展 T> 通配符

java - 在java中扩展类并实现接口(interface)

java - 我应该如何在Java中引用 "grandparent"类的函数/void?

HTML - 我可以将 <template> 标签放在哪里而不引起 W3C 验证器的提示?

html - 删除网格项时缩小网格布局中的间隙

html - 如何在容器底部设置元素

css - Bootstrap 表没有换行

html - 删除导航栏上方的空白