【问题标题】:How do I select the “last child” of element with specific class name in CSS?如何在 CSS 中选择具有特定类名的元素的“最后一个子元素”?
【发布时间】:2020-01-30 03:05:14
【问题描述】:

我有一个 HTML 表格,我想要最后一行,不包括 .table-bottom 类的行,并删除它的 border-bottom

<table cellpadding="0" cellspacing="0" style="width:752px;" class="table">
    <tr class="table-top"><th style="width:40px;">ID</th> <th>Post title</th> <th>Date</th> <th>Owner</th> <th style="width:100px;">Actions</th></tr>
    <tr class="table-middle"><td>1</td> <td>Test</td> <td>16.7.2013</td> <td>Admin</td> <td>Delete</td></tr>
    <tr class="table-middle"><td>2</td> <td>Test 2</td> <td>3.8.2013</td> <td>Admin</td> <td>Delete</td></tr>
    <tr class="table-bottom"><td colspan="5"></td></tr>
</table>
.table tr th {
color:#929191;
font-weight:bold;
font-size:14px;
text-align:left;
padding-left:19px;
border-right:1px solid #d7d7d7;
border-bottom:1px solid #d7d7d7;
}
.table tr th:last-child {
border-right:none;
}
.table tr td {
color:#929191;
font-weight:bold;
font-size:12px;
padding:19px;
border-right:1px solid #d7d7d7;
border-bottom:1px solid #d7d7d7;
}
.table tr td:last-child {
border-right:none;
}
.table .table-middle:last-of-type td {
border-bottom:none;
background:red;
}
.table tr.table-middle td:first-child {
text-align:center;
}
.table tr th:first-child {
text-align:center;
padding-left:0px;
}
.table tr.table-bottom td {
border-right:none;
border-bottom:none;
}

但是.table .table-middle:last-of-type td 选择器不起作用。

这里是Fiddle

有什么想法吗?

【问题讨论】:

标签: html css css-selectors


【解决方案1】:

简答:你不能通过 CSS 来做到这一点,除非你改变你的 HTML 结构。

更多详情:

:last-of-type伪类,表示其父元素的子元素列表中其类型的最后一个兄弟元素。

考虑一下这个选择器:.table-middle:last-of-type

虽然.table-middle 正确指向元素,但具有.table-middle 类的元素不是其type 的最后一个兄弟,因为type 一词指的是HTML 元素本身,而不是@987654329 的组合@。


这是可行的解决方案:

HTML

<table cellpadding="0" cellspacing="0" style="width:752px;" class="table">
    <thead>
        <tr>
            <th style="width:40px;">ID</th>
            <th>Post title</th>
            <th>Date</th>
            <th>Owner</th>
            <th style="width:100px;">Actions</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="5"></td>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td>1</td>
            <td>Test</td>
            <td>16.7.2013</td>
            <td>Admin</td>
            <td>Delete</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Test 2</td>
            <td>3.8.2013</td>
            <td>Admin</td>
            <td>Delete</td>
        </tr>        
    </tbody>
</table>

CSS

.table tbody tr:last-child td {
    border-bottom:none;
    background:red;
}

这里是Fiddle

【讨论】:

  • 当最后一行似乎有 colspan 时使用 tfoot 是一个很好的建议。
【解决方案2】:

完全不用类也可以让它变得更简单:

HTML:

<table>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
</table>

CSS:

table {
    width: 500px;
}
table td {  /* style for all the cells between the top and bottom row */
    background: red;
}
table tr:first-child td { /* style for the cells in the top row */
    background: green;
}
table tr:last-child td { /* style for the cells in the bottom row */
    background: blue;
}
table tr:first-child td:first-child, /* first row, first cell */
table tr:first-child td:last-child, /* first row, last cell */
table tr:last-child td:first-child, /* last row, first cell */
table tr:last-child td:last-child { /* last row, last cell */
    background: yellow;
}

同时检查working JSFiddle

【讨论】:

  • 选择.table-middle表的最后一行没有帮助。
  • 确实如此。但是由于 startpost 代码中的最后一个表格行,我认为不需要它。但这取决于 TS 来决定。
猜你喜欢
  • 2016-01-16
  • 2014-02-12
  • 2011-11-10
  • 2011-07-14
  • 1970-01-01
  • 2023-03-04
  • 2011-10-31
  • 2015-02-07
相关资源
最近更新 更多