【发布时间】:2015-12-20 01:41:48
【问题描述】:
我在当前项目中使用 sass,但不知道为什么它无法编译:
.section {
&:first-of-type {
background: url("../img/misc/.jpg") center center no-repeat;
background-size: cover;
&--text {
&--title {
/*THIS WON'T COMPILE*/
}
&--subtitle {
/*THIS WON'T COMPILE*/
}
&--subscribe {
&--input {
/*THIS WON'T COMPILE*/
}
&--button {
/*THIS WON'T COMPILE*/
}
}
}
}
&:nth-of-type(2) {
&--text {
&--title {
/*THIS WON'T COMPILE*/
}
&--paragraph {
/*THIS WON'T COMPILE*/
}
}
}
}
我收到此错误: “&--text”的父选择器无效:“Home stylesheet .section:nth-of-type(2)”
这是我的html:
<section class="section"> <!-- Cover section -->
<div class="section--text">
<h1 class="section--text--title">
Main title
</h1>
<p class="section--text--subtitle">
Some text
</p>
<div class="section--text--subscribe">
<input class="section--text--subscribe--input" type="email" placeholder="Your email">
<button class="section--text--subscribe--button">button</button>
</div>
</div>
</section>
<section class="section"> <!-- Problem section -->
<img src="img/ipad.jpg" alt="App screenshot">
<div class="section--text">
<h2 class="section--text--title">
Title
</h2>
<p class="section--text--paragraph">
Some text
</p>
</div>
</section>
我不能使用嵌套将 css 规则应用于伪选择器的子代吗?我在这里错过了什么?
非常感谢!
【问题讨论】:
-
它会编译成类似:
.section:first-of-type--text,而我假设你想要类似.section:first-of-type .section--text? -
是的,这就是我的目标!所以我将我的 sass 更改为:
.section:first-of-type { .section--text { // some css &--title { //some css } } }(我缩短了代码以便将其放入评论部分,但希望你明白我的意思?)但它仍然无法正常工作。我不明白。它正在编译为:.section:first-of-type .section--text {//some css}.section:first-of-type .section--text--title {//some css}等。
标签: css sass css-selectors