&放;.不在SCSS工作......为什么不呢?

时间:2018-06-02 18:19:36

标签: css sass flexbox styling scss-lint

我正在写一些基本的SCSS。但是我注意到了追加&在课前,只在某些情况下有效。例如,在" section"之类的元素之前或之前":悬停,活跃"。

但是在#34; .class"之前,这对我来说永远不会适用。为什么不呢?

例如,在下面的代码中, .flex-item 样式仅适用,如果我删除&amp ;.但是看看docs / internet,它应该与&在前面,没有?

section {
  padding: 20px;
  margin: 0 auto;
  border: 1px solid red;
  position: relative;
  width: 95%;
  list-style: none;
  top: 100px;

  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;

  -webkit-flex-flow: row wrap;
  justify-content: center;


  &.flex-item {
    border: 1px solid green;
    padding: 5px;
    width: 50%;
    height: 150px;
    margin-top: 10px;

    line-height: 150px;
    color: $white;
    font-weight: bold;
    font-size: 3em;
    text-align: center;
  }
}

和这里的JSX:

我基本上想要一个部分,(因为它有很高的应用)。然后是两个柔性物品,它们与柔性容器并排排列......

   <section className="flex-container">
    <div className="flex-item">
      <h1>A cashback credit card for holiday and home</h1>
      <ul className="pros">
        <li>No fees for making purchases abroad</li>
        <li>Nothing added on top of the Mastercard exchange rate</li>
        <li>Manage your holiday spending with realtime in-app notifications</li>
        <li>0.5% cashback on all purchases above £1</li>
        <li>Friendly customer support from anywhere with with in-app chat</li>
      </ul>
      <p>Great the same great rewards on holiday and at home with the Travel Cashback Credit Card.</p>
      <p>See full information</p>

      <Link to="/page-2/">Go to page 2</Link>
    </div>
    <div className="flex-item">
      <h1>Hi people</h1>
      <p>Welcome to your new Gatsby site.</p>
      <p>Now go build something great.</p>
      <Link to="/page-2/">Go to page 2</Link>
    </div>
  </section>
);

export default IndexPage;

1 个答案:

答案 0 :(得分:1)

只需移除&之前的.flex-item

section {
    // your section rules
    .flex-item {
        // your .flex-item rules
    }
}

在您想要使用伪选择器,将类添加到当前选择器(或部分类名),链接&或执行操作的情况下,您只需要:hover(父元素引用)一些其他父选择器用法:

a {
    // your link styles
    &:hover {
        // your hover styles
    }
}

.button {
    // .button styles
    &.colored {
        // .button.colored styles
    }
    &-large {
        // .button-large styles
    }
}

或其他一些父选择器设备:

section {
    // some styles
    .parentclass & {
         // this will match .parentclass section
    }
}

这只是几个例子。