The Stylus @extend directive is inspired by (and essentially the same as) the SASS Implementation, with few subtle differences. This feature significantly simplifies maintenance of semantic rulesets that inherit from other semantic rulesets.
“Extending” with mixins
Although you can use mixins to achieve a similar effect, this can lead to duplicate CSS. A typical pattern is to define several classes as shown below, then combine them on the element such as “warning message”.
While this technique works just fine, it’s difficult to maintain.
To produce this same output with @extend, simply pass it the desired selector (note that @extend and @extends are equal, one is just an alias of another). Stylus will then append the .warning selector to the existing .message ruleset. The .warning class then inherits properties from .message.
Where Stylus currently differs from SASS is, SASS won’t allow @extend nested selectors:
form button padding: 10px a.button @extend form button Syntax error: Can't extend form button: can't extend nested selectors on line 6 of standard input Use --trace for backtrace.
With Stylus, as long as the selectors match, it works!
form input[type=text] padding: 5px border: 1px solid #eee color: #ddd textarea @extends form input[type=text] padding: 10px
Note that if the selector is not extended, it won’t be in the resulting CSS, so it’s a powerful way to create a library of extendable code. While you can insert code through mixins, they would insert the same code every time you use them, while extending placeholders would give you compact output.
Optional extending
Sometimes it might be usefull to be able to extend something that might exists or not depending on the context. You can suffix any selector by !optional to achieve this:
本文详细介绍了一种利用 ESP8266 01S 模块构建 Web 服务器的成功实践方案。通过具体的代码示例和详细的步骤说明,帮助读者快速掌握该模块的使用方法。在疫情期间,作者重新审视并研究了这一未被充分利用的模块,最终成功实现了 Web 服务器的功能。本文不仅提供了完整的代码实现,还涵盖了调试过程中遇到的常见问题及其解决方法,为初学者提供了宝贵的参考。 ...
[详细]