【问题标题】:Does SCSS (SASS) selector order matter for nested classes嵌套类的 SCSS (SASS) 选择器顺序是否重要
【发布时间】:2017-11-27 09:38:19
【问题描述】:

这里我有 SCSS 来设置列表项的样式。我想知道的是类和伪选择器的选择顺序。所以基本上,&:before.active 是否等于 &.active:before

这是后者的完整示例:

.sidebar-list {
  list-style-type: none;
  padding: 0;

  & li {
    padding: 4px 0;

    &:before {                      // Here,
      color: darken($font-color, 60%);
      font-family: "FontAwesome";
      content: "\f101\00a0";
    }

    &.selected:before {             // And here.
      color: darken($font-color, 30%);
    }
  }
}

以及重要部分的前者(在li内):

    &:before {                 // Here,
      color: darken($font-color, 60%);
      font-family: "FontAwesome";
      content: "\f101\00a0";

      &.selected {             // And here. This would evaluate to "li:before.selected"
        color: darken($font-color, 30%);
      }
    }

对于列表项的:before psuedo-selector 样式,哪一个是正确的?

谢谢!

【问题讨论】:

    标签: css sass css-selectors


    【解决方案1】:

    是的,顺序很重要。 li:before.selected 基本会被忽略,因为它是无效的。

    这里有一个 sn-p 示例:

    span::before {
      content:'span::before (Normal)';
      background-color: #ddd;
    }
    
    /* Valid */
    span.ribbon::before {
      content: "span.ribbon::before (Valid)";
      background-color: #0f0;
    }
    
    /* Invalid. Will be ignored */
    span::before.ribbon {
      content: "span::before.ribbon (Invalid)";
      background-color: #f00;
    }
    <span></span>
    <span class="ribbon"></span>

    此外,您需要为 ::before 伪元素使用双冒号(在 CSS3 中更新)。

    【讨论】:

    • CSS 伪选择器(或内容)只需要一个冒号。只有伪元素需要::。 (我查了一下以确保:stackoverflow.com/a/41867706/2512078)谢谢!这是一个很好的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 2014-07-31
    • 2012-06-20
    • 2022-11-08
    • 2014-10-21
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多