代码之家  ›  专栏  ›  技术社区  ›  sliptype

Sass父选择器插值

  •  1
  • sliptype  · 技术社区  · 6 年前

    & 在sass中,选择器将输出父选择器两次:

    .a {
        &__element {
              #{ & }--modifier { // Why does this output .a__element .a__element--modifier ??
                color: blue;
              }
        }
    }
    
    .b {
        &__element {
              & &--modifier {
                color: blue;
              }
        }
    }
    

    Compiles to :

    .a__element .a__element--modifier {
      color: blue;
    }
    
    .b__element .b__element--modifier {
      color: blue;
    }
    

    .a__element--modifier . 有什么解释吗?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Hooman    6 年前

    这个 & #{} 语法。

    要获得预期的输出,请使用:

    .a {
        &__element {
            &--modifier {
                color: blue;
            }
        }
    }
    
        2
  •  2
  •   Tigran    6 年前
    .a {
        &__element {
              &--modifier {
                color: blue;
              }
        }
    }
    

    将编译为

    .a__element--modifier {
      color: blue;
    }
    

    插值只是插入 & 我们的情况就是这样 .a__element ,然后构造选择器。