带有类选择器的 CSS last-child

标签 css css-selectors

http://jsfiddle.net/SJyV3/

HTML:

<div id="parent">
    <div class="tt">tt</div>
    <div class="tt">tt</div>
    <div class="huh">huh</div>
</div>

CSS:

.tt:nth-child(2) {
    color:blue;
}
#parent .tt:last-child {
    color:red;
}

是否可以选择具有提供类的最后一个子元素?

最佳答案

 div.tt:last-child {
    background:yellow;
}

实际上选择了最后一个 child 。但是,如果它不是父类中的最后一个实例,则它不会选择类的最后一个实例。

因此您必须将 HTML 更改为:

<div id="parent">
    <div id="ttwrppaer">
        <div class="tt">tt</div>
        <div class="tt">tt</div>
        <div class="tt">tt</div>
    </div>
    <div class="hhhd">tt</div>
</div>

这是一个 demo

关于带有类选择器的 CSS last-child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299851/

相关文章:

javascript - 前置所有 CSS 选择器

css - 从我们的组件 Angular 覆盖外部库的样式

css - 下拉框未完全显示(所有浏览器)

html - 无法相对于复选框垂直对齐标签

javascript - CSS HTML 最后一个 child 不工作?

css - 分组 .className + 媒体查询?

css - 混淆两种不同的 CSS 语法来选择嵌套元素

javascript - 从 2 个不同的 HTML 文件导入 Div 并添加其内容

html - 如何在另一个 "a"标签内使用 "a"标签?

使用 CSS 转义的具有数字 id 的元素的 jQuery 选择器