css - 如何删除SCSS中的伪选择器?

标签 css sass nested selector extend

在 SCSS 中,我使用 @extend 将 2 个选择器合并为 1 行,但伪选择器(:before) 是在 #bar 内部创建的,如下所示:

#foo {
  position: relative;
  &:before {
    width: 200px;
  }
}
#bar {
  @extend #foo;
}

输出:

#foo, #bar {
  position: relative; 
}
#foo:before, #bar:before {
  width: 200px; 
}

我想用SCSS来做这样的事情:

#foo, #bar {
  position: relative; 
}
#foo:before {
  width: 200px; 
}
/* I want to remove #bar:before */

有没有办法通过创建@extend来删除不需要的伪选择器?

最佳答案

您可以placeholder selector(%)将共享属性声明为 #foo#bar

See example

使用如下:

%shared {
  position: relative;
}
#foo{
   @extend %shared;
    &:before {
    width: 200px;
    content:'';
  }
}
#bar {
    @extend %shared;
}

关于css - 如何删除SCSS中的伪选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53424689/

相关文章:

html - 如何在图像上设置垂直

css - 内容填充第一列然后换行到下一列

python - 嵌套到 Python 中的动态深度

javascript - DevExpress/至尊dxSelectBox

html - 如何在彼此之间垂直对齐多个元素

html - 如何在旋转时扩大立方体的边?

javascript - 仅选取并显示大于0的值

javascript - 如何暂停并调用嵌套 for 循环内的函数?

css - Bootstrap 模态适合宽度和水平居中

reactjs - 如何将 scss 文件导入到 typescript 文件中