css - 在less中将伪类添加到父祖先(选择器)

标签 css css-selectors less

有没有一种方法可以将伪类添加到嵌套子元素的父元素中。 仅使用 CSS

示例:在 .less 文件中,这就是我所拥有的。

.collection {
  // Some styling
    .headingRow {
        //Some styling
        .heading{
           //Some styling
           // This is where i want it to add the styling for alternate .collection class

         }
     }
 }

这就是我想要的输出

.collection:nth-of-type(2n+1) .headingRow .heading 
{
    background-color:  #7a003d; 
}
.collection:nth-of-type(2n) .headingRow .heading
{
    background-color:  #322f31;
}

这是我尝试过的 - 从 .heading 类添加 &,我可以使用类似

的东西添加父元素或类
    .collection {
  // Some styling
    .headingRow {
        //Some styling
        .heading{
           div&
           // This results in div.collection .headingRow .heading { }
         }
     }
 }

有没有办法可以将伪类添加到父祖先?

最佳答案

像这样:

.collection {
    // ...
    .headingRow {
        // ...
    }
}

.headingRow .heading {
    .collection 
        & {background-color: red}
    .collection:nth-of-type(2n) 
        & {background-color: blue}
    .collection:nth-of-type(2n + 1)
        & {background-color: green}
}

虽然我不认为它比没有任何嵌套的普通旧 CSS 定义更好。

关于css - 在less中将伪类添加到父祖先(选择器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299347/

相关文章:

javascript - 编译后的less.js编译文件在哪里?

javascript - 视频在设备中没有响应

html - 如何在带有文本的div内创建一个不可见的方 block ?

css - bootstrap 4.1 - 仅获取网格系统

html - 使用 bootstrap 还是主题作弊?

jquery - 悬停时的 CSS - 将元素 ID1 更改为 ID2

css - 无点引擎解析器返回空字符串

css - 如何在 CSS 中选择任何以某个单词开头的元素

animation - 每个子元素都有延迟的 CSS 动画

twitter-bootstrap - 在 WinLess 上编译 twitterbootstrap buttons.less 时出错