【发布时间】:2021-11-07 05:58:04
【问题描述】:
我有一个简单的表格标记:
<div class="table">
<div class="table__headers"></div>
<div class="table__row"></div>
<div class="table__row"></div>
</div>
我已经使用这个 CSS 设置了它的样式:
<style lang="scss" scoped>
.grid {
display: grid;
grid-gap: 0.5rem;
}
.table {
&__headers,
&__row {
@extend .grid;
padding: 0.5rem 0.75rem;
:first-child {
text-align: start;
}
:last-child {
text-align: end;
}
p {
font-size: 0.8rem;
font-weight: bold;
text-align: center;
margin: 0;
}
}
&__row {
background: #f8f8fa;
}
}
</style>
我唯一的问题是我无法专门定位 first 和 last table__row 元素,因此我可以设置不同的样式。
以下不起作用:
&__row:first-of-type {
border: solid 1px red;
}
&__row:last-of-type {
border: solid 1px blue;
}
last-of-type 有效,但不是第一个类型。任何帮助将不胜感激!
谢谢,
【问题讨论】:
-
因为这不是这些伪类的工作方式(如您所料)。一个元素必须匹配所有条件才能被选择器选中。由于
table__row绝不是其父级中的第一个 div,因此它永远不会匹配.table__row:first-of-type选择器。
标签: javascript vue.js sass