It looks like you can search
.text-body > :nth-last-child(1 of p, ul, h1, h2, h3, h4, h5)
from Selectors 4 (originally indicated as :nth-last-match()
). This limits the list of possible matches to only these types of elements and selects the last occurrence of them in the parent .text-body
element. Illustration:
<div class="text-body"> <h1></h1> <p></p> <ul></ul> <h2></h2> <p></p> <span></span> </div>
There are six children in this example, five of which are any p, ul, h1, h2, h3, h4, h5
. The last element of the five is p
, which immediately precedes the span
, so it matches the selector above. h1
will correspond to the equivalent expression :nth-child()
, and span
will never match the expression specified in the selection list - in fact, the span
itself can be expressed as :not(p, ul, h1, h2, h3, h4, h5)
.
While :nth-child()
:nth-last-child()
and :not()
were introduced in Selectors 3, the selector list argument is new to Selectors 4. But no one has implemented it yet and no one knows , when it will be. Unfortunately, there is no equivalent using what is currently available, since it is basically the same as this question , but instead of the class selector, you are looking for the nth (last) child matching the option pool. In both situations, you are dealing with the nth occurrence of some subset of children.
It is best to use JavaScript, for example, add a class to the last instance among these element types when loading the page. Something like this with the native DOM / Selectors APIs:
var types = document.querySelectorAll('.text-body > p, .text-body > ul, .text-body > h1, .text-body > h2, .text-body > h3, .text-body > h4, .text-body > h5'); types[types.length - 1].className += ' last';
... this is pretty nasty compared to the following jQuery:
$('.text-body').children('p, ul, h1, h2, h3, h4, h5').last().addClass('last');
note that
:nth-last-child(1 of p, ul, h1, h2, h3, h4, h5)
not equivalent
:last-child:matches(p, ul, h1, h2, h3, h4, h5)
since the latter corresponds to the last child of his parent, if and only if he is one of these types of elements. In other words :last-child:matches(...)
is the equivalent of Selectors 4 p, ul... { &:last-child { ... } }
(second part of Harry's answer).