【问题标题】:How do I directly extend an SCSS parent selector?如何直接扩展 SCSS 父选择器?
【发布时间】:2019-09-22 17:20:17
【问题描述】:

我有以下代码:

article.featured {
  h4 {
    margin-bottom: 20px
  }
  :first-child {
    height: 100%;
    padding-top: 0;
    padding-left: 0;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    .img {
      @extend &;
    }
  }
  :last-child {
    padding-top: 0;
    padding-right: 0
  }
}

这是13

@extend &;

问题是当我尝试编译这个样式表时,我得到以下错误:

Parent selectors aren't allowed here.
  ╷
10│     @extend &;
  │             ^
  ╵
  stdin 13:19 root stylesheet on line 10 at column 19

如何解决此错误并使用父 (&) 元素的属性扩展 .img 元素?

【问题讨论】:

    标签: css sass extend extending


    【解决方案1】:

    我发现当我将.img 选择器移动到:first-child 选择器旁边并将选择器更改为:first-selector, :first-selector .img 时它可以工作。

    这是工作代码(注意:3):

    article.featured {
      h4 { margin-bottom: 20px }
    
      :first-child, :first-child .img {
        height: 100%;
        padding-top: 0;
        padding-left: 0;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
      }
    
      :last-child {
        padding-top: 0;
        padding-right: 0
      }
    }
    

    编译为:

    article.featured h4 {
      margin-bottom: 20px;
    }
    
    article.featured :first-child, article.featured :first-child .img {
      height: 100%;
      padding-top: 0;
      padding-left: 0;
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    article.featured :last-child {
      padding-top: 0;
      padding-right: 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2013-09-16
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多