作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 BEM Scss 请解释如何在第 n 个子元素中进行选择。
我尝试了以下格式,但对我不起作用
<div class="bookearly-container" >
<div class="row bookearly-container__row">
<div class="col-xs-12 col-md-4 bookearly-container__col">
<div class="bookearly-container__discountcontainer">
</div>
<div class="bookearly-container__discountcontainer">
</div>
<div class="bookearly-container__discountcontainer">
</div>
</div>
</div>
</div>
.bookearly-container {
&__row {
margin-bottom: 4.6rem;
& > :nth-child(3) {
&__discountcontainer { -- this element not selected
&:before {
display: none;
}
}
}
}
}
最佳答案
您正在 >
中使用子组合器 ( .bookearly-container__row
) (CSS 示例中的第 4 行),唯一的直接子级是 .bookearly-container__col
.
如果您想针对 .bookearly-container__discountcontainer
您需要稍微调整选择器嵌套的元素。
尝试使用 @debug
指令结合 &
选择器查看您实际选择的内容,这在您没有其他线索时很有帮助。
这是解决问题的直接建议:
.bookearly-container {
@debug &; // .bookearly-container
&__row {
@debug &; // .bookearly-container__row
}
&__discountcontainer:nth-child(3) {
@debug &; // .bookearly-container__discountcontainer:nth-child(3)
&:before {
@debug &; // .bookearly-container__discountcontainer:nth-child(3):before
}
}
}
>
),您可以将它嵌套在
&__col
中。选择器,像这样:
.bookearly-container {
&__col {
// Targeting any class ending with "__discountcontainer"
& > [class$="__discountcontainer"]:nth-child(3) {
&:before {
@debug &; // .bookearly-container__col > [class$="__discountcontainer"]:nth-child(3):before
}
}
}
}
关于sass - 如何在Element BEM Scss中选择第n个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52643012/
我是一名优秀的程序员,十分优秀!