css - 如何过滤 tr 最后一个 child 而不是第一个 child

标签 css css-selectors

请参阅下面的示例。是否可以将底部边框应用于 <tr><thead> ,通过使用 CSS 选择器?

编辑:这是唯一的一行。也有可能没有<thead><tbody>完全没有。

table {
    border-collapse: collapse;
}
caption, th, td {
    text-align: left;
    line-height: 1.4;
    padding: 8px;
}
tr {
    border-bottom: 1px solid #ccc;
    vertical-align: top;
}
tr:last-child {
    border-bottom: 0;
}
<h2>Tables</h2>
<table>
    <caption>Optional table caption.</caption>
    <thead>
        <tr>
            <th>#</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Username</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="row">1</th>
            <td>Mark</td>
            <td>Otto</td>
            <td>@mdo</td>
        </tr>
        <tr>
            <th scope="row">2</th>
            <td>Jacob</td>
            <td>Thornton</td>
            <td>@fat</td>
        </tr>
        <tr>
            <th scope="row">3</th>
            <td>Larry</td>
            <td>the Bird</td>
            <td>@twitter</td>
        </tr>
    </tbody>
</table>

最佳答案

您可以使用 :not() 的组合/:only-child伪类,以防止选择元素(如果它是唯一的子元素):

Updated Example

tr:last-child:not(:only-child) {
    border-bottom: 0;
}

在这种情况下,您还可以将 :only-child 替换为 :first-child,因为您想要否定第一个子级。

Updated Example

tr:last-child:not(:first-child) {
    border-bottom: 0;
}

您还可以缩小选择器范围,以便仅选择 tbody 中的 tr 元素:

Example Here

tbody tr:last-child {
    border-bottom: 0;
}

..或者您可以在 thead 内的 tr 元素上显式设置 border。..

关于css - 如何过滤 tr 最后一个 child 而不是第一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597867/

相关文章:

jquery - jquery 属性选择器找不到的属性

html - 歌剧中未显示下拉图像

c++ - QComboBox 文本颜色没有按预期更改

CSS 选择器将样式应用于除第一个图像之外的所有图像

css - 我可以使用两个类似于 ".foo>a.bar>a"的子选择器 ".foo.bar"吗?

javascript - UI 自动化中选择器的 self 修复

css - 多个匹配子串选择器

java - 使用忽略大小写的属性查找元素

javascript - 在不丢失宽高比的情况下调整图像大小

html - 为什么html元素只在所有css文件加载完成后才显示