如何使用 SCSS 选择父级

时间:2021-02-10 14:21:24

标签: css sass scss-mixins

我想在 SCSS 中写这样的东西:

.username {
  color: red;

  @something .mobile {
    font-weight: bold
  }
  
  @something .desktop {
    font-weight: normal
  }
}

...在 CSS 中有以下输出:

.mobile .username {
  color: red;
  font-weight: bold
}


.desktop .username {
  color: red;
  font-weight: normal;
}

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:5)

在课后使用 parent selector sealed class,如下所示:

&

这是一个demo

SS

注意:不需要重复.username { color: red; .mobile & { font-weight: bold } .desktop & { font-weight: normal } }