Sass Extend&Inheritance
本文最后更新于:2021年2月19日 晚上
Sass Extend&Inheritance
This is one of the most useful features of Sass. Using @extend
lets you share a set of CSS properties from one selector to another. It helps keep your Sass very DRY. In our example we’re going to create a simple series of messaging for errors, warnings and successes using another feature which goes hand in hand with extend, placeholder classes. A placeholder class is a special type of class that only prints when it is extended, and can help keep your compiled CSS neat and clean.
SCSS SYNTAX
1 |
|
CSS OUTPUT
1 |
|
What the above code does is tells .message
, .success
, .error
, and .warning
to behave just like %message-shared
. That means anywhere that %message-shared
shows up, .message
, .success
, .error
, & .warning
will too. The magic happens in the generated CSS, where each of these classes will get the same CSS properties as %message-shared
. This helps you avoid having to write multiple class names on HTML elements.
You can extend most simple CSS selectors in addition to placeholder classes in Sass, but using placeholders is the easiest way to make sure you aren’t extending a class that’s nested elsewhere in your styles, which can result in unintended selectors in your CSS.
Note that the CSS in %equal-heights
isn’t generated, because %equal-heights
is never extended.
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!