【问题标题】:Switch icons on the :after parent element using & when the class is different当类不同时,使用 & 切换 :after 父元素上的图标
【发布时间】:2018-08-07 14:31:06
【问题描述】:

我正在尝试以父框元素为目标来更改 :after Pseudo 元素中的图标。当我将 & 符号放在类之后,它会跳出: .box-example-default #pageContainer .box .box-header 这不是你应该如何使用它吗?任何建议

<div id="pageContainer">
    <div class="box box-example box-example-default">
        <div class="header">
            <h1>Title<h1>
        </div>
    </div>

    <div class="box box-example box-example-simple">
        <div class="header">
           <h1><h1>
        </div>
    </div>
 </div>
.box {
    .header{
        &:after{
         position: absolute;
         right:0;
         bottom:-63%;
          content: '\f0ce';
         display:block;
         font-family: "FontAwesome";

         .box-example-simple &{
              content: '\f0ce';
           }
        }
    }

【问题讨论】:

    标签: sass


    【解决方案1】:

    末尾的 & 符号对您不起作用,因为您需要同一级别的 .box.box-example-simple。看到这个post

    您可以使用@at-root#{&amp;} 来实现这一点,如下所示:

    .box {
        .header{
            &:after{
             position: absolute;
             right:0;
             bottom:-63%;
             content: '\f0ce';
             display:block;
             font-family: "FontAwesome";
    
            @at-root .box-example-simple#{&} {
                  content: '\f0ce';
               }
            }
        }
    }
    

    但是,把它写出来可能会更简洁:

    .box {
      .header {
        &:after {
          position: absolute;
          right:0;
          bottom:-63%;
          content: '\f0ce';
          display:block;
          font-family: "FontAwesome";
        }
      }
    
      &.box-example-simple {
        .header {
          &:after {
            content: '\f0ce';
          }
        }
      }
    }
    

    【讨论】:

    • 感谢您的回复,第一个示例没有做到这一点,它仍然会创建输出到:.box-example-default #pageContainer .box .box-header:after。我同意第二个例子更干净(这是我一直在使用的)。
    • 奇怪,如果你看here,它确实会编译为.box-example-simple.box .header:after#{$} 语法仅在 Sass 3.4 中有效。不管怎样,很高兴你能成功!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多