Sass extend with pseudo selectors

人盡茶涼 提交于 2019-11-29 07:23:43

When you want to extend a pseudo class or pseudo element, you just want to extend the parent selector (ie. everything that comes before the colon).

%foo:before {
  color: red;
}

.bar {
  @extend %foo;
}

Generates:

.bar:before {
  color: red;
}

So for your instance, what you want to do is this:

.icon-ab-logo, {
    font: 100%/1 'icomoon'; // use the shorthand
    speak: none;
    text-transform: none;
    -webkit-font-smoothing: antialiased;
}

%foo:before, .icon-ab-logo:before { //i want to reuse this.
    content: "\e000";
}

@mixin icon( $icon ){
    font: 100%/1 'icomoon'; // use the shorthand
    speak: none;
    text-transform: none;
    -webkit-font-smoothing: antialiased;
    @extend #{$icon};
}

.bar {
    @include icon('%foo');
}

Be aware that your mixin generates a lot of styles, so it's not really suitable for extensive reuse. It would make a lot more sense to be extending that as well.

Seems like SASS doesn't work so well with pseudo elements in extends.

Work around the issue like this:

%before-icon
  color: red

.foo
  /* Some styles here */

  &:before
    @extend %before-icon

Result:

.foo:before {
  color: red;
}

.foo {
  /* Some styles here */
}

Also, it looks like you're overcomplicating things. You'll find your code difficult to grasp and maintain.

PS You should keep mixins in _mixins.scss.

As said before, you should try to use placeholder class. In other hand always first consider @mixin first and try to avoid nesting with @extend.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!