简体   繁体   English

嵌套的SCSS并定位父对象

[英]Nested SCSS and targeting the parent

I nest SCSS quite a lot and I'm able to target the parent element using & but in this instance it's giving me a headache! 我嵌套了很多SCSS,并且可以使用&定位父元素&但是在这种情况下,这让我很头疼! I feel like I'm missing something quite obvious but I can't get it to work. 我觉得我似乎遗漏了一些明显的东西,但我无法使其正常工作。 Here's my (simplified) SCSS: 这是我的(简化的)SCSS:

[class^="icon--"]:before,
[class*=" icon--"]:before {
    opacity: .50;
}

Basically I'm using an icon font and I want the icons (added using the :before element) to appear lighter than the text it's attached to - fine. 基本上,我使用的是图标字体,我希望图标(使用:before元素添加)看起来比其附加的文本更亮-很好。

Now I want to overwrite this on certain elements this class is applied to. 现在,我想在应用该类的某些元素上覆盖此内容。 I've tried to following (below), which does remove the opacity from the targeted element ...but it also seems to cancel out the opacity: .50; 我尝试遵循(如下),它确实从目标元素中消除了不透明度...但是它似乎也消除了opacity: .50; on every element. 在每个元素上。 So basically everything has opacity: 1; 因此,基本上所有东西都具有opacity: 1; set. 组。

[class^="icon--"]:before,
[class*=" icon--"]:before {
    opacity: .50;

    @at-root {

        .btn#{&} {
            opacity: 1;
        }
    }
}

Any ideas? 有任何想法吗?

What you want is the following SCSS: 您想要的是以下SCSS:

[class^="icon--"],
[class*=" icon--"] {
  &:before {
    opacity: .50;
  }    
  &.btn  {
    &:before {
      opacity: 1;
    }
  }
}

which renders to this CSS: 呈现为此CSS:

[class^="icon--"]:before,
[class*=" icon--"]:before {
  opacity: .50;
}
[class^="icon--"].btn:before,
[class*=" icon--"].btn:before {
  opacity: 1;
}

& references the selector that will be built so far in your level of nesting (not necessarily only the parent, only if nesting level is one). &引用到目前为止将在您的嵌套级别中构建的选择器(只有嵌套级别为1时,不一定是父级选择器)。

Your attempt would create this CSS: 您的尝试将创建以下CSS:

[class^="icon--"]:before,
[class*=" icon--"]:before {
  opacity: .50;
}
[class^="icon--"]:before.btn,
[class*=" icon--"]:before.btn {
  opacity: 1;
}

Which would expect a class .btn on a pseudo element :before (which you cannot have). 它将在伪元素:before (您不能拥有)上期望类.btn

You can easily check on sassmeister.com . 您可以轻松地在sassmeister.com检查 Here is a really helpful article on the almighty ampersand in SCSS . 这是有关SCSS中的全能“&”号的非常有用的文章。

I'm not really familiar with SCSS but it looks like you're trying to target 我对SCSS不太熟悉,但看起来您正在尝试定位

[class*=" icon--"]:before.btn

A class btn in a pseudo-element, this might be the problem 伪元素中的btn类,这可能是问题

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

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