简体   繁体   English

将CSS应用于特定类的最后一个元素

[英]Apply CSS to last element of specific class

I want to apply some CSS to the last blog-post. 我想在上一篇博文中应用一些CSS。 The problem is that the last element in the div 'blog-posts' is the same type of element as the 'blog-post' divs. 问题是div“blog-posts”中的最后一个元素与“blog-post”div的元素类型相同。

I've tried: 我试过了:

  • last-of-type 最后的型
  • last-child 最后一个孩子

HTML: HTML:

<div class="blog-posts">
    <div class="blog-post"></div>
    <div class="blog-post"></div>
    <div class="blog-post"></div>
    <div class="blog-post"></div>
    <div class="f03-456245"></div>
</div>

CSS: CSS:

.blog-post:last-child{
    //do some css
}

Last element using .class is not possible. 使用.class最后一个元素是不可能的。 In your case you can use nth-last-child property. 在您的情况下,您可以使用nth-last-child属性。

 .blog-posts div:nth-last-child(2) {
   background: #ff0000;
 }

DEMO DEMO

You may have to do like this: 你可能必须这样做:

.blog-posts div:last-child{
  //do some css
}

It is assuming div is the element. 假设div是元素。 It applies for anyother element type p , span etc... 它适用于任何其他元素类型pspan等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM