问题描述
如果子元素没有选择类,是否可以为父元素添加样式?
<style>
.parent:not(.parent > .child){
color: red;
}
</style>
<div class="parent">heading<span>1</span><div>
<div class="parent">heading<span class="child">2</span><div>
解决方法
不,CSS(和 LESS)从上到下工作,你不能遍历树。这意味着孩子不能决定父母的风格。
您必须在父级中定义一个类或 ID。例如:
<div class="parent modified-behaviour">Heading<span class="child">2</span><div>
LESS 编译为 CSS,因此也缺少父选择器。