【问题标题】:Sass .scss: Nesting and multiple classes?Sass .scss:嵌套和多个类?
【发布时间】:2012-06-20 12:51:09
【问题描述】:

我正在为我当前的项目使用 Sass (.scss)。

以下示例:

HTML

<div class="container desc">
    <div class="hello">
        Hello World
    </div>
</div>

SCSS

.container {
    background:red;
    color:white;

    .hello {
        padding-left:50px;
    }
}

这很好用。

我可以在使用嵌套样式时处理多个类吗?

在上面的示例中,我正在谈论这个:

CSS

.container.desc {
    background:blue;
}

在这种情况下,所有div.container 通常都是red,但div.container.desc 会是蓝色的。

如何使用 Sass 将其嵌套在 container 中?

【问题讨论】:

标签: sass


【解决方案1】:

可以使用parent selector reference&amp;,编译后会被父选择器替换:

你的例子:

.container {
    background:red;
    &.desc{
       background:blue;
    }
}

/* compiles to: */
.container {
    background: red;
}
.container.desc {
    background: blue;
}

&amp; 将完全解析,因此如果您的父选择器本身嵌套,则嵌套将在替换 &amp; 之前解析。

这种符号最常用于写pseudo-elements and -classes

.element{
    &:hover{ ... }
    &:nth-child(1){ ... }
}

但是,您几乎可以将&amp; 放置在您喜欢的任何位置*,因此也可以进行以下操作:

.container {
    background:red;
    #id &{
       background:blue;
    }
}

/* compiles to: */
.container {
    background: red;
}
#id .container {
    background: blue;
}

但是请注意,这会以某种方式破坏您的嵌套结构,因此可能会增加在样式表中查找特定规则的工作量。

*:&amp; 前面不允许出现除空格以外的其他字符。所以你不能直接连接selector+&amp; - #id&amp; 会抛出错误。

【讨论】:

  • 附带说明,&amp; 的常见用法是在使用伪元素和伪类时。例如:&amp;:hover.
  • @crush 为了完整起见,我将其添加到我的答案中。感谢您的评论。
  • 谢谢。我以为我很愚蠢,因为基础指南中没有提到它!顺便说一句,文档已将 URL 移至:sass-lang.com/documentation/…
  • 如果&amp; 用在一行的末尾,它会将该行放在所有其他级别的其余类的开头。您可以通过在.container 下执行&amp;.desc 来组合元素,这会将.desc 附加到它,从而产生.container.desc
  • scss-lint 建议使用“&::hover”(双冒号)
【解决方案2】:

如果是这种情况,我认为您需要使用更好的方法来创建类名或类名约定。例如,正如您所说,您希望 .container 类根据特定用途或外观具有不同的颜色。你可以这样做:

SCSS

.container {
  background: red;

  &--desc {
    background: blue;
  }

  // or you can do a more specific name
  &--blue {
    background: blue;
  }

  &--red {
    background: red;
  }
}

CSS

.container {
  background: red;
}

.container--desc {
  background: blue;
}

.container--blue {
  background: blue;
}

.container--red {
  background: red;
}

上面的代码基于类命名约定中的 BEM 方法。你可以查看这个链接:BEM — Block Element Modifier Methodology

【讨论】:

  • 注意这听起来不错,但应该不惜一切代价避免。当您尝试在您的项目中搜索 .container--desc 并最终没有结果时,您会感谢我的。
【解决方案3】:

克里斯托夫的回答是完美的。但是,有时您可能想上更多的课而不是一门。在这种情况下,您可以尝试@at-root#{} css 功能,这将使两个根类可以使用&amp; 并排放置。

这行不通(由于nothing before &amp; 规则):

container {
    background:red;
    color:white;
    
    .desc& {
      background: blue;
    }

    .hello {
        padding-left:50px;
    }
}

但这会(使用@at-root plus #{&amp;}):

container {
    background:red;
    color:white;
    
    @at-root .desc#{&} {
      background: blue;
    }

    .hello {
        padding-left:50px;
    }
}

【讨论】:

    【解决方案4】:

    使用&amp;

    SCSS

    .container {
        background:red;
        color:white;
    
        &.hello {
            padding-left:50px;
        }
    }
    

    https://sass-lang.com/documentation/style-rules/parent-selector

    【讨论】:

      【解决方案5】:

      除了 Cristoph 的回答之外,如果您想在声明中更加具体,您可以引用容器类组件的所有子组件。这可以通过以下方式完成:

      .container {
      // ...
        #{&}.hello {
           padding-left: 50px;
        }
      }
      

      这编译为:

      .container .container.hello {
         padding-left: 50px;
      }
      

      希望对你有帮助!

      【讨论】:

        【解决方案6】:

        这对我有用

        <div class="container">
          <div class="desc">
            desc
          </div>
          <div class="asc">
            asc
          </div>
        </div>
        
        .container{
          &.desc {
            background: blue;
          }
          &.asc {
            background: red;
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-27
          • 1970-01-01
          • 2015-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多