Sass @extend base/default without also extending pseudo-classes?

[亡魂溺海] 提交于 2019-12-18 12:47:15

问题


I know I can @extend .foo:hover, but is there a way to @extend the .foobar base/default properties without also extending the definitions for pseudo-classes like :hover, :active, etc?

For example, how would I change the following such that .foobar extends only .foo's default state?

.foo {
    & {
         color:blue;
    }
    &:hover {
         background-color: black;
    }
}

.foobar {
     @extend .foo;
     &:hover {
          //As is, I have to override. Any better way?
          background-color: transparent;
     }
}

(If there is no way to do this with Sass, is there a preferred way to achieve the same effect?)


回答1:


You have to rewrite your selectors in such a way that you only extend exactly the part you want:

%foo {
    color:blue;
}

.foo {
    @extend %foo;

    &:hover {
         background-color: black;
    }
}

.foobar {
    @extend %foo;

    &:hover {
         background-color: transparent;
    }
}

However, depending on how you are going to be extending/reusing your .foo class, the new @content directive might be the better way to go.

@mixin foo {
    color: blue;

    &:hover {
        @content;
    }
}

.foo {
    @include foo {
        background-color: black;
    }
}

.foobar {
    @include foo {
        background-color: transparent;
    }
}


来源:https://stackoverflow.com/questions/11963501/sass-extend-base-default-without-also-extending-pseudo-classes

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