css - 第 n 个 child 及之前

标签 css css-selectors pseudo-element

我遇到了 CSS 问题,我需要解决这个问题

article div#comments-wrapper ul.sub-comment:before {
    width:1px;
    height:67px;
    background-color:#e4e4e4;
    position:absolute;
    margin-left:-595px;
    margin-top:-36px;
    content:'';
}
article div#comments-wrapper ul.sub-comment:nth-child(1):before {
    height:37px;
    margin-top:-6px;
}

但是我不能像这样放置两个伪元素,我已经测试过了(不起作用), 也尝试了其他一些方法,但没有设法解决。

最佳答案

:nth-child() 不会按类别或任何内容进行过滤。在您的代码中,您的第一个 ul.sub-comment 不是 #comments-wrapper 中的第一个子项,因此它不起作用。

相反,使用 this selector technique并反转您的 heightmargin-top 样式,如下所示:

article div#comments-wrapper ul.sub-comment:before {
    width:1px;
    height:37px; /* was 67px in your code */
    background-color:#e4e4e4;
    position:absolute;
    margin-left:-595px;
    margin-top:-6px; /* was -36px in your code */
    content:'';
}
article div#comments-wrapper ul.sub-comment ~ ul.sub-comment:before {
    height:67px; /* was 37px in your code */
    margin-top:-36px; /* was -6px in your code */
}

基本上,不要使用 :nth-child(1)(或 :first-child),而是使用带有另一个 ul 的兄弟选择器。 sub-comment 将原始样式应用到所有后续 ul.sub-comment 元素在第一个元素之后

Updated fiddle (还反转了 background-color 样式,所以第一个保持蓝色)

关于css - 第 n 个 child 及之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9846886/

相关文章:

html - Bootstrap 进度条 Div

css - 选择 CSS 中的每个第 N 个元素

css - 将 CSS 类添加到 Shiny 的 textOutput

css - 使用伪 :Before Trick, 添加空格的漂亮标题形状

html - 一个div,相互独立,在整个body的中间偏左,高度变为

css - 工具提示覆盖在容器组件上

html - 当应用于子 div 时悬停停止工作,当位置设置为绝对时按钮消失

css - 如何将 css 规则基于 dir 属性的继承值

css - 如何为 :before and :after pseudo elements? 启用 -webkit-animation/transition-property

html - 如何使用 CSS 在输入元素后添加换行符?