html - 如何在less中添加通用样式以及基于动态类添加不同样式
问题描述
下面是我有一个公共类的 HTML 签名,我想将相同的颜色属性应用于两个 div 元素。但是基于动态添加的背景类,我想为其添加背景颜色。尽管给定的 LESS 样式有效,但我试图重构不起作用的代码。
HTML:
<div class="common background-red"></div>
<div class="common background-warning"></div>
LESS: (which is working, please suggest if it can be refactored)
.background-red {
color: blue;
background-color: red;
}
.background-warning {
color: blue;
background-color: yellow;
}
我尝试了下面的代码,但它不工作
.common {
color: blue
.background-red {
background-color: red;
}
.background-warning {
background-color: yellow;
}
}
解决方案
您可以使用 & 字符将一个类添加到一个类中,如下所示:
.common {
color: blue
&.background-red {
background-color: red;
}
&.background-warning {
background-color: yellow;
}
}
可以在此处查看更多信息: https ://css-tricks.com/the-sass-ampersand/
推荐阅读
- python - 如何在kivy中的应用程序文件中保存和加载设置?
- sql - 如何在 SQL 中获得两个表的并集
- java - Java 是 OOP 语言。我们可以通过创建对象来调用它,也可以像 POP 一样使用它。但这有什么问题呢?
- google-cloud-platform - GCP 上的 WordPress 实例上的 HTTPS 连接被拒绝
- c# - Xamarin 错误:找不到所引用的对象
- c# - 如何让两个插件互相接收和发送消息?
- python - 图形工厂不会在 colab 中加载
- python - tkcalendar TclError:未知选项“-17/06/2020”
- python - 仅在 AWS 实例上阻止使用 Python 进行抓取 - 阻止请求
- c++ - 为什么初始化 std::vector 时不能使用模板整数?