Comments on: :last-of-type https://css-tricks.com Tips, Tricks, and Techniques on using Cascading Style Sheets. Wed, 01 Jun 2022 20:53:48 +0000 hourly 1 https://wordpress.org/?v=6.1.1 By: George Jempty https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1612622 Sun, 22 Oct 2017 18:31:22 +0000 http://css-tricks.com/?page_id=14224#comment-1612622 As for the dispute as to whether last-of-type works on classes or not, or only on elements, why not specify an element with a class? The following worked perfectly for me — hope this helps somebody.

.accent-image-selector {
  padding-bottom: 1rem;
}

div.accent-image-selector:last-of-type {
  padding-bottom: 0rem;
}
]]>
By: Tony Scialdone https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1606062 Wed, 11 Jan 2017 14:16:40 +0000 http://css-tricks.com/?page_id=14224#comment-1606062 Here’s what I’m looking for:

.className a:first-of-type[href*=”string”]

I can’t get it to work with either first-of-type or last-of-type. Has anyone been able to get this working, or is using both selectors simply not supported?

]]>
By: Azarel https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1604963 Mon, 14 Nov 2016 01:27:28 +0000 http://css-tricks.com/?page_id=14224#comment-1604963 In reply to Alfonse.

@TylerH @Jan Hartmann Ok so you’re both right and wrong at the same time here lads. This is really weird behavior so bear with me.

It appears that using a class with :last-of-type results in something that I can only describe as *.class:first-of-class. So what exactly happens here? Well it does bind last-of-type to the html tag. Whether it be a div or a p or whatever. But that ‘last’ element must also have the css tag which is why Jan’s example doesn’t result in anything highlighted because whilst it does bind to the class as in this rule is applied using the class selector to any html elements the actual :last-of-type is bound to the html tags so you can’t have multiple classes using the same html tag and expact multiple :last-of-type highlights.

Now, this doesn’t happen to affect me because I’m writing web components which means that my element tag names are largely unique meaning that the class selector syntax is very helpful for filtering styling within lists and such.

]]>
By: Jan Hartmann https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1602280 Fri, 17 Jun 2016 10:39:14 +0000 http://css-tricks.com/?page_id=14224#comment-1602280 In reply to Alfonse.

@TylerH: You’re wrong:

https://jsfiddle.net/6cd83wb4/2/

]]>
By: TylerH https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1601649 Fri, 20 May 2016 21:17:31 +0000 http://css-tricks.com/?page_id=14224#comment-1601649 In reply to Alfonse.

You’re mistaken. :last-of-type works on class names and IDs just fine. The term “element” in the spec you quoted doesn’t refer to “tag name”, it refers to a general “element”. How you select that element is irrelevant. Example: https://jsfiddle.net/6cd83wb4/1/

]]>
By: Andy https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1598582 Wed, 16 Dec 2015 10:28:28 +0000 http://css-tricks.com/?page_id=14224#comment-1598582 Try this:

.theList>p:nth-last-of-type(1) {
background: #ff0000;
}

where .theList is your container element

]]>
By: Kristina https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1596372 Tue, 11 Aug 2015 21:10:54 +0000 http://css-tricks.com/?page_id=14224#comment-1596372 Question, is it possible to create link that shows the lastest img from the latest article in one specific category?

I want to have one picture in my header that will change and my readers have the possibility to read more if they are interested.

For example: I have several categories and I want a preview on my index page but only the first picture from the latest article in one of those categories..

hard to explain but several bloggers use that type of preview right now and I want that as well, but I don’t use wordpress so I have to find a way to solve that problem..

please help!

(inthefrow.com, lilypebbles.co.uk, viviannadoesmakeup.com, heyclaire.com – you get the picture..)

kind regards Kristina

]]>
By: Neha https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1596356 Mon, 10 Aug 2015 20:03:43 +0000 http://css-tricks.com/?page_id=14224#comment-1596356 Below worked for me:

.PFourTiles{
margin: 20px 13px 0px 0px;
}
.pseudo-primary{
.PFourTiles:last-of-type {
margin: 20px 0px 0px 0px;
}
}

]]>
By: Alfonse https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1596303 Wed, 05 Aug 2015 20:36:39 +0000 http://css-tricks.com/?page_id=14224#comment-1596303 So I tested this out and apparently it ONLY applies to HTML elements and not by class-names unfortunately.

In the spec it states:

The :last-of-type CSS pseudo-class represents the last sibling with the given element name in the list of children of its parent element.

So doing something like this won’t work:

.foo:last-of-type {color: red;}

But doing this will work:

p:last-of-type {color:red;}
]]>
By: David https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1592076 Fri, 23 Jan 2015 04:36:20 +0000 http://css-tricks.com/?page_id=14224#comment-1592076 Just started a web design class and I am having the same problem. How do I fix it?

]]>
By: Sten Hougaard https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1591981 Sat, 17 Jan 2015 21:43:57 +0000 http://css-tricks.com/?page_id=14224#comment-1591981 Too bad that “last-of-type” is not actually implemented correctly.

A type should be what is specified before the :last-of-type, so if I have “.January” as the “type”, the “.January:last-of-type” should hit the last occurrence elements with the class “.January“, which it does not (using Chrome 39.0.2171.95 (64-bit), OS X).

]]>
By: Paul Sanders https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1587426 Wed, 10 Dec 2014 15:42:10 +0000 http://css-tricks.com/?page_id=14224#comment-1587426 It looks like these *-of-type selectors can be disrupted (in Chrome at least) by dynamically generated elements inserted into the parent container. For instance, if some js code inserts a div into the parent, your original last-of-type will lose track of the element to which it was attached.

]]>
By: Robert https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1581759 Tue, 27 May 2014 22:43:56 +0000 http://css-tricks.com/?page_id=14224#comment-1581759 Thanks! I had a shady memory of this CSS-“trick” and had to check your site. Result! :)

]]>
By: Miguel https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-1581154 Tue, 13 May 2014 23:24:55 +0000 http://css-tricks.com/?page_id=14224#comment-1581154 Marcos is right about last-of-type in Chrome it doesn’t work when I refer to a class. Thanks for that!

]]>
By: Marcos https://css-tricks.com/almanac/selectors/l/last-of-type/#comment-580355 Wed, 09 Oct 2013 10:33:20 +0000 http://css-tricks.com/?page_id=14224#comment-580355 In reply to Brian FitzGerald.

“last-of-type” only refers to the element, not the attributes …

]]>