引用SCSS中元素的父级

时间:2020-07-26 15:12:59

标签: sass

我想要这段代码:

ul li {
    ...

    &:first-child a {
        ...
    }

    &:last-child a {
        ...
    }

    &.active a {
        ...
    }

    a {
        ...
    }
}

要这样:

ul li {
    ...
   
    a {
        ...

        &:first-child a ...
    }
}

有没有办法引用父级(&应该是ul li)?也许甚至可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

像这样使用。对类link做任何想做的事情 如果要在锚点下锚点,则是html验证错误。您必须避免那样。

ul {
  li {
    &.link {
      &:first-child {
        a {
          color: red;
        }
      }
      &:last-child {
        a {
          color: red;
        }
      }
    }
  }
}

输出

ul li.link:first-child a {
  color: red;
}
ul li.link:last-child a {
  color: red;
}