如何使用Stylus精简混合CSS选择器集合?
Alright, let's break down practical, Stylus-friendly ways to streamline your selector list—no extreme minification required, just clean, maintainable code aligned with Stylus's core logic:
Group shared prefixes with nesting
Stylus excels at nesting, which lets you eliminate repetitive prefixes. For your grouped selectors with shared starting classes:// Instead of .cl_nw, .cl_n, .cl_ne .cl_ &_nw, &_n, &_ne // Your styles here // Instead of .alert_nw, .alert_n, .alert_ne .alert_ &_nw, &_n, &_ne // Your styles here // Instead of .selected .color_fg1, .selected .color_fg2, .selected .color_fg3 .selected .color_fg1, .color_fg2, .color_fg3 // Your styles hereCombine child selectors under shared parents
For selectors that share a parent chain (like your.menuPopupListitems), nest child selectors to avoid repeating the full parent path:// Instead of .menuPopupList li.selected, .menuPopupList li.selected a .menuPopupList li.selected &, & a // Your styles hereUse placeholder selectors for reusable style patterns
If multiple selector groups share identical styles, define a placeholder selector once and extend it wherever needed. This cuts down on redundant style declarations:%shared-highlight-style // Common styles (e.g., color, font-weight) .cl_ &_nw, &_n, &_ne @extend %shared-highlight-style .selected .color_fg1, .selected .color_fg2, .selected .color_fg3 @extend %shared-highlight-styleSimplify long compound selectors with variables
That unwieldyul.checklists li.list_item span.tasks span.progress bselector can be stored in a variable to make your code cleaner and easier to update later:$taskProgressBase = ul.checklists li.list_item span.tasks span.progress $taskProgressBase b // Your styles here
内容的提问来源于stack exchange,提问作者ultracrepidarian

