简体   繁体   English

Sass&嵌套与伪选择器

[英]Sass Ampersand nesting with pseudo selectors

Trying to utlize the SASS Ampersand to get the following css output. 尝试使用SASS Ampersand以获得以下CSS输出。 It is not working when we use Ampersand with inside pesudo selector. 当我们将Ampersand与内部pesudo选择器一起使用时,它不起作用。

CSS CSS

.test:first-child .test-image { display: block; }

SASS 上海社会科学院

.test {
    &:first-child {
        display: inline-block;
        &-image {
            display: block;
        }   
    }
}

Above code basically cascading the -image with first-child. 上面的代码基本上将-image与第一个孩子层叠在一起。

This is because the ampersand is just concatenating the parent with the child. 这是因为“&”号只是将父级与子级连接在一起。 If you want the compiled CSS to look like your example you need to do this: 如果要使已编译的CSS看起来像您的示例,则需要执行以下操作:

.test {
    &:first-child &-image{
      display: block;     
    }
}

If you are trying to achieve 如果您想实现

.test:first-child .test-image { display: block; }

With your code it is getting compiled as this 随着您的代码,它正在这样编译

.test:first-child-image {
    display: block;
}

Instead ,you can simply write it as this . 相反,您可以简单地这样写。

.test:first-child {
        .test-image {
            display: block;
        }   
}

Hope it helps 希望能帮助到你

It sounds like you have mistaken how the ampersand works in Sass. 听起来您好像误会了&符号在Sass中的工作方式。 The ampersand is essentially a shorthand for writing each outer selector and then adding the selector after it onto the end. “&”号本质上是写每个外部选择器,然后将选择器添加到末尾的简写形式。 As .test-image is distinct from .test , you should specify it as follows. 由于.test-image.test不同,因此您应按以下说明进行指定。

.test {
    &:first-child {
        .test-image {
            display: block;
        }   
    }
}

Compiles to 编译为

.test:first-child .test-image {
    display: block;
}

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

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