You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Stylus精简混合CSS选择器集合?

Streamlining Your CSS Selector List for Stylus

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 here
    
  • Combine child selectors under shared parents
    For selectors that share a parent chain (like your .menuPopupList items), 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 here
    
  • Use 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-style
    
  • Simplify long compound selectors with variables
    That unwieldy ul.checklists li.list_item span.tasks span.progress b selector 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:29:03