css - 更少的嵌套元素

标签 css nested less

我知道

div {
  a {
    ...
  }
}

在LESS中代表

div a {
  ...
}

在 CSS 中。问题是 LESS 代码是什么:

div > a {
  ...
}

最佳答案

您可以简单地使用

div {
  > a{
    color: blue;
  }
}

div {
  & > a{ /* & represents the current selector parent, so will be replaced with div in this case in the output */
    color: blue;
  }
}

两者具有相同的效果,并会产生以下编译输出。

div > a {
  color: blue;
}

注意:您也可以对 + (相邻同级组合器)和 ~ (一般同级组合器)采用相同的方法。

关于css - 更少的嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25078707/

相关文章:

html - 将文本对齐到 div 的中间

javascript - 通过路径访问嵌套属性

java - 嵌套类可以实例化外部类吗?

css - SASS 与 LESS - 制作 WordPress 主题哪个更好?

javascript - 同一 LESS 文件中的多个 'for' 循环

html - ember js中的粘性页脚

html - 如何在表格单元格 `td` 之间添加空格?

html - 轮播元素中的绝对元素位于下一个元素下

PHP - 避免嵌套的 if 语句 - 更简洁的编码方式

syntax-highlighting - IDE 语法高亮支持 LESS