【问题标题】:SCSS nesting is looking for a childSCSS嵌套正在寻找一个孩子
【发布时间】:2016-11-01 19:07:55
【问题描述】:

我遇到了 SCSS 的问题,这可能只是因为缺乏经验。我正在尝试使我的stylesheet.scss 文件为具有特定类的任何图像提供样式,然后基于其他类具有更多样式,如下所示

SCSS:

img.postimage {
  height: 150px;

  .expand {
    transition: all .2s ease-in-out;
    &:hover {
      transform: scale(1.1);
    }
  }
}

烦人的是,这不会像下面这样编译 CSS:

img.postimage {
  height: 150px;
}

img.postimage.expand {
  transition: all .2s ease-in-out;
}

img.postimage.expand:hover {
  transform: scale(1.1);
}

它实际上是这样生成的:

img.postimage {
  height: 150px;
}

image.postimage .expand { /* note the space, it's actually looking for a .expand child */
  transition: all .2s ease-in-out;
}

image.postimage .expand:hover {
  transform: scale(1.1);
}

我不擅长 SCSS,所以我不知道我做错了什么。

【问题讨论】:

    标签: css sass compass-sass compass


    【解决方案1】:

    你可以使用 & 来解决这个问题

    例如

    image {
       &.someclass {
          margin:10px;
       }
    }
    

    会变成

    image.someclass {
       margin: 10px;
    }
    

    【讨论】:

    • 啊,非常感谢。我刚刚在 Sassmeister 上进行了测试,现在一切都很好。我会尽快接受!
    • 没问题,我想每个以 Sass 开头的人都有这个问题,包括我自己。
    • 我应该真的想到它,因为我也与伪类(:hover 等)作斗争
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 2019-04-11
    • 2021-06-03
    • 1970-01-01
    • 2012-12-13
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多