CSS :last-child 选择器选择指定class最后一个元素,为什么不对?

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

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

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

代码如下:

html

<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>
</div>

css

body {
    background: black;
}

.comment {
    width:470px;
    border-bottom:1px dotted #f0f0f0;
    margin-bottom:10px;
}

.comment:last-of-type {
    border-bottom:none;
    margin-bottom:0;
}
(5)
上一篇 2019年2月21日 下午11:28
下一篇 2019年2月23日 下午10:43

相关推荐

发表评论

登录后才能评论