CSS 在 SASS 中是否可以从另一个文件中的类继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11787330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible in SASS to inherit from a class in another file?
提问by Dan Tao
The question pretty much says it all.
这个问题几乎说明了一切。
For instance, if I were using, say, Twitter Bootstrap, could I define classes in my own SASS stylesheet that inherit from Bootstrap's CSS classes? Or does inheritance in SASS only work within the scope of a single file?
例如,如果我使用Twitter Bootstrap,我可以在我自己的 SASS 样式表中定义从 Bootstrap 的 CSS 类继承的类吗?还是 SASS 中的继承仅在单个文件的范围内有效?
回答by agustibr
YES!its possible.
是的!这是可能的。
If you want all <button>
elements to inherit the .btn
class from Twitter Bootstrap's Default buttons
如果您希望所有<button>
元素都.btn
从Twitter Bootstrap 的默认按钮继承该类
In your styles.scss
file you would have to first import _bootstrap.scss
:
在您的styles.scss
文件中,您必须先导入_bootstrap.scss
:
@import "_bootstrap.scss";
Then below the import:
然后在导入下面:
button { @extend .btn; }
回答by Mark Ernst
**I might be mistaken, but if I get what you're trying to do, can't you just use the @extend .classname;
command inside the element that you'd want to extend? Naturally, you should only modify your own code to preserve updatability.
**我可能弄错了,但如果我明白你想要做什么,你不能只@extend .classname;
在你想要扩展的元素内使用命令吗?当然,您应该只修改自己的代码以保持可更新性。
回答by ScottS
To my knowledge, you have to use @import
of the file containing the classes you want to use into your SASS file in order to utilize them in that file. However, I am not a SASS/SCSS expert, so someone may know of another way to remotely use them that I am not aware of.
据我所知,您必须使用@import
包含要在 SASS 文件中使用的类的文件,以便在该文件中使用它们。但是,我不是 SASS/SCSS 专家,所以有人可能知道另一种我不知道的远程使用它们的方法。