【发布时间】:2021-02-19 05:36:24
【问题描述】:
[我知道我可能没有很好地解释标题中的问题。 ]
所以,我正在处理一个样式表,其中有两个自定义元素(<grid> 和 <box>)是弹性框,并在 flex-direction 中交替出现,例如:
<box> <!-- flex-direction: column -->
<grid> <!-- flex-direction: row -->
<box></box> <!-- flex-direction: column -->
<box></box> <!-- flex-direction: column -->
</grid>
<box> <!-- flex-direction: row -->
<grid></grid> <!-- flex-direction: column -->
</box>
</box>
在较少的情况下,我最好的解决方案是:
grid, box { display: flex }
body {
> grid, > box {
flex-direction: column;
> grid, > box {
flex-direction: row;
> grid, > box {
flex-direction: column;
// Etc...
}}}
}
这很糟糕,css中是否有类似下面的代码?
grid, box { display: flex }
body > grid:parents(even), body > box:parents(even) { flex-direction: column }
body > grid:parents( odd), body > box:parents( odd) { flex-direction: row }
编辑:body > nth-child() 不起作用。
代码:
body > grid:nth-child(even),
body > box:nth-child(even) {
flex-direction: column;
}
body > grid:nth-child(odd),
body > box:nth-child(odd) {
flex-direction: row;
-
body > element:nth-child(odd)只选择body的第一个直接子节点,应该是body element:nth-child(odd)。 -
我在问题开头的 HTML 示例在修改以解决问题 1 时使用上面的代码如下所示。
<box> <!-- flex-direction: column -->
<grid> <!-- flex-direction: row -->
<box></box> <!-- flex-direction: column -->
<box></box> <!-- flex-direction: row -->
</grid>
<box> <!-- flex-direction: column -->
<grid></grid> <!-- flex-direction: row -->
</box>
</box>
这不是一回事。
【问题讨论】:
标签: html css flexbox css-selectors less