&.在 SCSS 中不起作用……为什么不呢?
&. doesn't work in SCSS... why not?
我正在编写一些基本的 SCSS。但我注意到在 class 之前附加 & 仅在某些情况下有效。例如,在像 "section" 或“:hover, active”这样的元素之前。
但这在“.class”之前对我不起作用。为什么不呢?
例如,在下面的代码中,如果我删除了 &,则 .flex-item 样式仅适用。但是看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:
我基本上想要一个部分,(因为它有很高的应用)。然后是两个 flex 项目,它们并排排成一排,带有 flex 容器...
<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;
只需删除 .flex-item
:
之前的 &
section {
// your section rules
.flex-item {
// your .flex-item rules
}
}
你只需要 &
(父元素引用)在你想使用伪选择器的情况下,添加 classes 到当前选择器(或部分 class 名称), link a :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
}
}
这只是几个例子。
我正在编写一些基本的 SCSS。但我注意到在 class 之前附加 & 仅在某些情况下有效。例如,在像 "section" 或“:hover, active”这样的元素之前。
但这在“.class”之前对我不起作用。为什么不呢?
例如,在下面的代码中,如果我删除了 &,则 .flex-item 样式仅适用。但是看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:
我基本上想要一个部分,(因为它有很高的应用)。然后是两个 flex 项目,它们并排排成一排,带有 flex 容器...
<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;
只需删除 .flex-item
:
&
section {
// your section rules
.flex-item {
// your .flex-item rules
}
}
你只需要 &
(父元素引用)在你想使用伪选择器的情况下,添加 classes 到当前选择器(或部分 class 名称), link a :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
}
}
这只是几个例子。