It seems like you're in search of
.text-body > :nth-last-child(1 of p, ul, h1, h2, h3, h4, h5)
mentioned in the Selectors 4 specification (initially introduced as :nth-last-match()
). This selector narrows down possible matches to specific element types and selects the last occurrence of those within the parent element, .text-body
. Here is an example:
<div class="text-body">
<h1></h1>
<p></p>
<ul></ul>
<h2></h2>
<p></p>
<span></span>
</div>
In this scenario, there are six children, five of which belong to any of p, ul, h1, h2, h3, h4, h5
. The last element from these five is the p
preceding the span
, therefore fulfilling the given selector. The h1
would match a similar expression with :nth-child()
, while the span
will never satisfy such criteria based on the selector-list — in fact, the span
can be represented as :not(p, ul, h1, h2, h3, h4, h5)
.
Although :nth-child()
, :nth-last-child()
, and :not()
were introduced in Selectors 3, the selector-list argument is a novelty in Selectors 4. However, no implementation currently exists for it, leaving its availability uncertain. Unfortunately, there is no equivalent alternative with current tools since it's fundamentally comparable to this question, except instead of a class selector, it targets the nth (last) child matching a group of options. Both scenarios involve working with the nth occurrence of a subset of an element's children.
You could resort to using JavaScript to, for instance, assign a class to the final instance among these element types during page load. A native DOM/Selectors APIs approach would look like this:
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';
... which might seem complex compared to the following jQuery solution:
$('.text-body').children('p, ul, h1, h2, h3, h4, h5').last().addClass('last');
Remember that
:nth-last-child(1 of p, ul, h1, h2, h3, h4, h5)
is not equal to
:last-child:matches(p, ul, h1, h2, h3, h4, h5)
as the latter only applies if the last child of its parent belongs to any of those specified element types. Essentially, :last-child:matches(...)
corresponds to
p, ul... { &:last-child { ... } }
(referring to Harry's response).