【问题标题】:Sass nesting of h1 within a .title selector.title 选择器中 h1 的 Sass 嵌套
【发布时间】:2017-09-19 09:47:41
【问题描述】:

我试过 sass

.title {
    font-weight: bold;
    // more title styles
    &h1 {
        font-size: 30px;
    }
}

生成的 css 是:

.titleh1 {
    font-weight: bold;
    // more title styles
}
.titleh1 {
    font-size: 30px;
}

无论如何我可以将 h1 嵌套在 .title 中以提供这样的 css 输出吗?

.titleh1 {
    font-weight: bold;
    // more title styles
}
h1.title {
    font-size: 30px;
}

【问题讨论】:

    标签: sass


    【解决方案1】:

    是的,您只需要the @at-root directive 即可跳出嵌套选择器:

    .title {
        font-weight: bold;
        // more title styles
    
        @at-root {
          h1#{&} {
            font-size: 30px;
          }
        }
      }
    

    【讨论】:

    • 嗨,谢谢 Seth,编译为 h1 .title { font-size: 30px; },我正在寻找编译为 h1.title { font-size: 30px; } 的东西(h1 和 .title 之间没有空格)
    • @Davey 将h1 & 更改为h1#{&}
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2017-09-15
    • 1970-01-01
    • 2021-08-26
    • 2012-09-22
    相关资源
    最近更新 更多