css - 我如何使用 css last-child?

标签 css css-selectors

我有一个评论列表,我想使用一些像这样的 css 代码评论 div 来设置最后一个 child 的 border-radius comment form
我正在使用此代码,但它不起作用。

 #comments>.comment:last-child, .indented>.comment:last-child{border-bottom-right-radius: 5px;}

它在像这样与 jquery 一起使用时有效

 $('#comments>.comment:last, .indented>.comment:last').css('border-bottom-right-radius', '10px');

但我想用 css 代码解决它。有什么建议吗?

简单代码的 fiddle Fiddle

最佳答案

我已经能够使用此 CSS 代码使其工作(对于提供的 HTML 结构):

#comments > .comment:nth-last-child(-n+2),
.indented > .comment:nth-last-child(-n+2) {
    border-bottom-right-radius: 15px;
}

现场演示: http://jsfiddle.net/93tZV/3/

因此,我们不使用 :last-child 来选择最后一个 child ,而是使用 :nth-last-child(-n+2) 来选择最后一个两个 child 。如果最后一个子元素是 .indented DIV,这会起作用,因为我们的 .comment 选择器会将其过滤掉。但是,如果最后两个子项都是 .comment DIV,则 CSS 样式将应用于它们,这会导致 http://jsfiddle.net/93tZV/4/。我不知道如何解决这个问题。

关于css - 我如何使用 css last-child?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12448281/

相关文章:

html - flexbox 如何在溢出时扩展到它的 child ?

html - Css 同级 (~) 选择器不适用于选中的属性

css - 什么 CSS 选择器可以用来选择 Bootstrap 工具提示?

html - CSS内联 block 舍入错误?

html - 页脚重叠内容/文章

javascript - 使用 maxWidth 时通过 JavaScript 检测图像宽度

html - 需要帮忙。基本的 HTML/CSS。标题不变色

html - 使用 Selenium Webdriver 从 <span> 类型的下拉框中选择一个值

css - 悬停时平滑的 CSS 旋转动画

php - php文件phpEclipse中的html/css/jquery/javascript代码完成