代码之家  ›  专栏  ›  技术社区  ›  john Gu

如何引用scs中的嵌套类

  •  0
  • john Gu  · 技术社区  · 3 年前

    我有以下.scs文件:=

    .singleNews {
      text-decoration: none;
      font-family: $font-family;
      font-size: $font-size;
      font-weight: $regular-font-weight;
    
      &__image {
        padding: 5em;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
    
        &.featured {
          height: 75%;
        }
      }
    }
    

    那么我该如何定义特色呢?我尝试了以下操作,但全部失败:-

    import styles from './SingleNews.module.scss';
    //code goes here...
    <div className={styles.singleNews__image__featured} style={{ backgroundImage: `url(${post.image})` }}/> 
    <div className={styles.singleNews__image featured} style={{ backgroundImage: `url(${post.image})` }}/> 
    <div className={styles.singleNews__image.featured} style={{ backgroundImage: `url(${post.image})` }}/> 
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Lundstromski    3 年前

    &.featured 使用类选择父选择器 featured ,这意味着div需要具有类 singleNews__image 作为特色的

    .singleNews__image.featured {
      background-color: blue;
      width: 100px;
      height: 100px;
    }
    
    /* Same selector with Sass */
    /*
    .singleNews {
      &__image {
        &.featured {
          background-color: blue;
          width: 100px;
          height: 100px;
        }
      }
    }
    */
    <div class="singleNews__image featured">
    </div>