相关疑难解决方法(0)

CSS可以检测元素的子元素数吗?

我可能回答了我自己的问题,但我非常好奇.

我知道CSS可以选择父项的单个子项,但如果其父项具有一定数量的子项,则可以支持对容器的子项进行样式化.

例如

container:children(8) .child {
  //style the children this way if there are 8 children
}
Run Code Online (Sandbox Code Playgroud)

我知道这听起来很奇怪,但是我的经理让我检查一下,没有找到任何东西,所以我决定在结束搜索之前转向SO.

css css-selectors

285
推荐指数
8
解决办法
17万
查看次数

当7个或更多孩子时,CSS First Child

所以我有这个查询来获取last-child有7个或更多元素的时间

li:nth-last-child(7) ~ li:last-child {
    background: red;
}
Run Code Online (Sandbox Code Playgroud)

只要存在最少的元素,这适用于任意数量的元素7.我想做的是相反的,得到的first-child.

我尝试了以下内容

li:nth-last-child(7) ~ li:first-child {
    background: red;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.奇怪的是,我可以使用以下内容获取第二个子元素

li:nth-last-child(7) ~ li:nth-child(2) {
    background: red;
}
Run Code Online (Sandbox Code Playgroud)

我知道这是非常复杂的CSS,甚至可能不可能,但我想知道它是否可以完成.如果可能的话,我宁愿不使用JS.我猜这是一个挑战;)

html css css-selectors css3

5
推荐指数
1
解决办法
580
查看次数

标签 统计

css ×2

css-selectors ×2

css3 ×1

html ×1