代码之家  ›  专栏  ›  技术社区  ›  erosman

在嵌套中重构CSS伪元素

css
  •  0
  • erosman  · 技术社区  · 2 年前

    可以 :not(:hover) 在下面的CSS中进行重构,以避免重复?

    a.label {
      &[href*="/abc"]:not(:hover),
      &[href*="/def"]:not(:hover),
      /* ... */
      &[href*="/xyz"]:not(:hover) {
        color: #f50;
      }
    }
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   SyndRain    2 年前

    移动 :not(:hover) 对父母来说完全没问题。

    parent-selector {
      &selector1, &selector2 {}
    }
    

    相当于

    :is(parent-selector)selector1, :is(parent-selector)selector2 {}
    

    在您的情况下, :is(a.label):not(:hover)[href] (原件)相当于 :is(a.label:not(:hover))[href] (已重构)。

    a.label:not(:hover) {
      &[href*="/abc"],
      &[href*="/def"] {
        color: #f50;
      }
    }
    <a class="label" href="./abc">Link1</a>
    <a class="label" href="./def">Link2</a>
    <a class="label" href="./xxx">Link3</a>