简体   繁体   English

基于祖父类更改伪元素样式

[英]Change pseudo-element style based on grandparent class

Consider the following scss: 考虑以下scss:

.link {
  ....

  span {
    .... 

    &:after {
      ....

      .link.active & {
        background-color: red;
      }

      .link:hover & {
        background-color: red;
      }

I want to change the background-color for the span :after pseudo-element when link is either being hovered or has the .active class. 我想更改span的背景颜色:after link要么悬停或具有.active:after伪元素:after

What I've tried ( the code posted above ) doesnt seem to work. 我试过的(上面发布的代码)似乎没有用。

Is there anything I'm missing ? 有什么我想念的吗?

you should try it like this scheme : 你应该像这个计划一样尝试:

.link {
  span {
    &:after {
     ...
    }
  }
  &.active,
  &:hover {
    span:after {
      background-color: red;
    }
  }
}

Working example : http://jsfiddle.net/92gqap5y/ 工作示例: http//jsfiddle.net/92gqap5y/

This question seems to be asked often, as mentioned in this thread . 似乎经常会问这个问题,如本主题所述

They increase the size of the link element to be as large as the span . 它们将link元素的大小增加到与span一样大。 Though I would recommend moving the :hover selector and .active class to the span element directly. 虽然我建议将:hover选择器和.active类直接移动到span元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM