如何在CSS中选择具有特定类名的元素的“最后一个子元素”?

时间:2013-08-18 12:03:46

标签: html css css3 css-selectors

我有一个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

任何想法?

2 个答案:

答案 0 :(得分:3)

简答:除非您更改HTML结构,否则您无法通过CSS执行此操作。

更详细信息:

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

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

虽然.table-middle正确指出了元素,但.table-middle类的元素不是其type的最后一个兄弟,因为type的术语是指HTML元素本身,而不是element.class的组合。


以下是工作解决方案:

<强> 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

答案 1 :(得分: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