写css的时候,经常需要选择一个类型的最后一个子元素,新手经常写成:last-child。

而:last-child所指的是元素是容器的最后一个子元素,而不是特定类型的元素的最后一个子元素时才有效。

我们可以使用:last-of-type来解决这个问题 last-of-type

代码如下:

<div class="commentList">
    <article class="comment " id="com21"></article>
    <article class="comment " id="com20"></article>
    <article class="comment " id="com19"></article>
    <div class="something"> hello </div>
body {
    background: black;
.comment {
    width:470px;
    border-bottom:1px dotted #f0f0f0;
    margin-bottom:10px;
.comment:last-of-type {
    border-bottom:none;
    margin-bottom:0;

(4)